猿问

通过计时器重置文本框文本并再次关注它,在javascript中

我正在使用 onclick 来重置和聚焦 Javasript 中的文本框文本。我想消除这个过程并实现 1.5 秒的计时器。所以文本框文本将自动消失,并且再次关注它。


function refresh()

  {

  var resetbutton = document.getElementById("myText");

  resetbutton.value="";

  resetbutton.focus();

  }


偶然的你
浏览 157回答 2
2回答

慕斯王

看起来您正在寻找一个 setInterval。试试这个代码:<!DOCTYPE html><html><body><p>Example</p><input id="myText" type="text" name="fname" value="delete this"><button onclick="myFunction()">Try it</button><script>&nbsp; function myFunction() {&nbsp; var asd = setInterval(refresh, 1500);&nbsp; function refresh()&nbsp; {&nbsp; &nbsp; var resetbutton = document.getElementById("myText");&nbsp; &nbsp; resetbutton.value="";&nbsp; &nbsp; resetbutton.focus();&nbsp; }}</script></body></html>我希望它有帮助!

当年话下

您可以使用 setTimeout() 然后清除该值并重新关注文本区域。下面你可以看到一个关于鼠标移出事件的例子。&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const resetbutton = document.getElementById("myText");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resetbutton.onmouseout = function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //1 second delay&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let delay = 1500;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTimeout(function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //clear value and re-focus&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resetbutton.value="";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resetbutton.focus();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, delay);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; </script>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答