javascript stoppropagation 不适用于 ajax .load

发布于 2024-12-19 01:46:02 字数 614 浏览 2 评论 0原文

当点击 时,我使用 .stopPropagataion() 来停止

  • 中的冒泡标签。
  • $(document).ready(function(){
       $(".article a").click(function(e) {
            e.stopPropagation();
       });
    });
    

    使用此 HTML:

    <li class="article" onlick="javascript:somefunction();">
       <div id="div_id">
          <a>Some link</a>
       </div>
    </li>
    

    一切正常,但随后我使用 $('#div_id').load("div_html.html") 将一些内容动态加载到 div 中。现在我的 .stopPropagation() 不会停止冒泡。为什么它不再起作用了?

    I used .stopPropagataion() to stop the bubbling in <li class="article"> when clicking on the <a> tag.

    $(document).ready(function(){
       $(".article a").click(function(e) {
            e.stopPropagation();
       });
    });
    

    With this HTML:

    <li class="article" onlick="javascript:somefunction();">
       <div id="div_id">
          <a>Some link</a>
       </div>
    </li>
    

    Everything worked fine, but then I used $('#div_id').load("div_html.html") to load some content into the div dynamically. Now my .stopPropagation() doesn't stop the bubbling. Why isn't it working anymore?

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(1

    陌路终见情 2024-12-26 01:46:02

    问题是,当您更新 div 的内容时,您会销毁其中的所有元素(包括附加到它们的任何事件处理程序)。您的代码正在将 click 事件挂接到 div 中的 a 元素当您的代码运行时 ; div 中的新 a 元素将不会被连接。

    在您的情况下,您最好使用 delegate (delegate ="http://jsbin.com/abizew/2" rel="nofollow">实时示例,带有 load):

    $(document).ready(function(){
       $(".article div").delegate("a", "click", function(e) {
            e.stopPropagation();
       });
    });
    

    挂钩 click改为 div。当单击发生时,它会从链接冒泡到 div,其中 jQuery 创建的单击处理程序会检查它以查看它是否实际上是对任何 a 元素的单击。如果是这样,它将停止点击。

    从 jQuery 1.7 开始,新的推荐语法是 on

    $(document).ready(function(){
       $(".article div").on("click", "a", function(e) {
            e.stopPropagation();
       });
    });
    

    请注意参数的顺序与delegate不同,但除此之外它做同样的事情。


    我建议避免将旧式 DOM0 处理程序(事件处理程序属性)与新式 DOM2 代码(附加了 jQuery 的处理程序)相结合。在许多浏览器中,这两种机制有些独立,并且可能无法像您希望的那样很好地协同工作。

    对此还有另外两条评论:

    onlick="javascript:somefunction();"
    
    1. 它是 onclick,而不是 onlick (您缺少 c
    2. 您不使用 javascript :事件处理程序属性上的伪协议,仅href。只需删除该部分即可。 (为什么它不会导致错误?因为纯粹是巧合,它是有效的 JavaScript - 您正在定义一个标签 你从未使用过。)

    The problem is that when you update the content of the div, you destroy all of the elements that were inside it (including any event handlers attached to them). Your code is hooking the click event on the a elements that are in the div as of when your code runs; new a elements in the div will not be hooked up.

    In your case, you're better off using delegate (live example, with load):

    $(document).ready(function(){
       $(".article div").delegate("a", "click", function(e) {
            e.stopPropagation();
       });
    });
    

    That hooks click on the div instead. When the click occurs, it bubbles from the link to the div, where the click handler created by jQuery examines it to see if it was actually a click on any a element. If so, it stops the click.

    As of jQuery 1.7, the new recommended syntax is on instead:

    $(document).ready(function(){
       $(".article div").on("click", "a", function(e) {
            e.stopPropagation();
       });
    });
    

    Note that the order of arguments is different from delegate, but other than that it does the same thing.


    I'd recommend avoiding combining old-style DOM0 handlers (event handler attributes) with new-style DOM2 code (your handler attached with jQuery). In many browsers, the two mechanisms are somewhat separate and may not play as nicely together as you'd like.

    Two other comments on this:

    onlick="javascript:somefunction();"
    
    1. It's onclick, not onlick (you're missing the c)
    2. You don't use the javascript: pseudo-protocol on event handler attributes, only hrefs. Just remove that part. (Why doesn't it cause an error? Because purely by coincidence, it's valid JavaScript — you're defining a label you never use.)
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文