简体   繁体   English

绑定工作但不工作

[英]Bind working but on not working

I am trying to get error on img load through on, but it doesn't work 我正在尝试通过img加载出错,但是它不起作用

$(document).on("error","#someid img",
               function(){
                  console.log("Image load error through On");
              });

but the similar works with 'bind' 但类似的工作与“绑定”

$("#someid img").bind("error", function() {
                         console.log("Image load error through bind");
                      });

The issue is that the error event doesn't bubble, so you can't use event delegation to capture it at the document level. 问题是error事件不会冒泡,因此您不能使用事件委托在document级别捕获它。

The only way to capture it will be to bind directly to the element. 捕获它的唯一方法是直接绑定到元素。


You can determine this by using the .bubbles property on the event object in your .bind() handler. 您可以通过在.bind()处理函数中的event对象上使用.bubbles属性来确定此问题。

$("#someid img").bind("error", function(event) {
    console.log(event.bubbles); // false
});
 $("#your_id").error(function () {
     alert("your text")
 })
  1. . on() replaces .bind(), .live() and .delegate() after jquery1.7. on() .bind(), .live() and .delegate()在jquery1.7之后替换.bind(), .live() and .delegate()
  2. .on() manages event delegation, bind doesn't. .on()管理事件委托,绑定则不。
  3. What is event.delegation ? 什么是event.delegation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM