代码之家  ›  专栏  ›  技术社区  ›  hu newhuan

触发焦点事件将触发模糊事件(不使用警报和浏览器控制台)

  •  2
  • hu newhuan  · 技术社区  · 7 年前

    jsfiddle link

    <input id = "focus-ipt" type = "text" value = "nothing" />

    var $focusIpt = document.querySelector( "#focus-ipt" );
    $focusIpt.addEventListener( "blur", function ( e ) {
       $focusIpt.value = "blured!";
    } );
    setTimeout( function () {
        $focusIpt.focus();
        }, 1000 );
    

    我只是触发聚焦事件,但在聚焦后触发模糊事件;

    1 回复  |  直到 7 年前
        1
  •  1
  •   Obsidian Age    7 年前

    这实际上是 案件; focus() 不会自动触发 blur() .

    这个 模糊() Run Tidy ),则 #focus-ipt 元素不再处于活动状态--您单击的按钮为。

    模糊() 模糊()

    var $focusIpt = document.querySelector("#focus-ipt");
    $focusIpt.addEventListener("blur", function(e) {
      $focusIpt.value = "Blurred!";
    });
    setTimeout(function() {
      $focusIpt.focus();
    }, 1000);
    <input id="focus-ipt" type="text" id="myText" value="Sample Text">
    <button type="button">Click me to lose focus</button>