首页 > jquery的trigger触发多次是什么原因?

jquery的trigger触发多次是什么原因?

  1. 描述你的问题
    为什么button,点击一次,内容会同时出现3次?原理是什么?

  2. 贴上相关代码

    <html>
    <head>
    <script type="text/javascript" src="/jquery/jquery.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
      $("input").select(function(){
        $("input").after("文本被选中!");
      });
      $("button").click(function(){
        $("input").trigger("select");
      });
    });
    </script>
    </head>
    <body>
    <input type="text" name="FirstName" value="Hello World" />
    <br />
    <button>激活 input 域的 select 事件</button>
    </body>
    </html>
  3. 相关截图

  1. 案例地址:
    http://www.w3school.com.cn/tiy/t.asp?f=jquery_event_trigger


stackoverflow有人问过这个问题,可以参考下参考连接

我测试了下,添加了阻止默认行为可以得到正确结果

$(document).ready(function(){
  $("input").select(function(e){
     e.preventDefault()
    $("input").after("文本被选中!");
  });
  $("button").click(function(){
    $("input").trigger("select");
  });
});
【热门文章】
【热门文章】