JQuery - 为什么Trigger方法会调用它三次?

use*_*627 13 javascript jquery

$(document).ready(function(){
    $("input").select(function(){
        $("input").after(" Text marked!");
    });
    $("button").click(function(){
        $("input").trigger("select");
    });
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>


<input type="text" value="Hello World"><br><br>

<button>Trigger the select event for the input field</button>
Run Code Online (Sandbox Code Playgroud)

资源

有人可以告诉我为什么选择事件在点击按钮后触发了三次?

似乎使用IE和Chrome可能会导致不同的结果.

Nip*_*una 1

在 IE 和 Firefox 中这工作得很好。仅在 Chrome 中注意到该问题

这似乎是因为事件被冒泡而发生的。如果您使用开发人员工具检查这一点,您可以看到更多信息

在此输入图像描述

如果您在最后一步断点处检查开发人员工具,您会注意到 isTrigger 属性为 true,这意味着它来自我们定义的触发器。

在此输入图像描述

开发人员工具的同一断点的接下来两次点击$("input").after(" Text marked!");显示了几乎相似的属性集。注意 bubbles 属性为 true

在此输入图像描述