2013-09-26 107 views
1

我想通过上得到IMG负载错误,但它不工作绑定工作,但在不工作

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

但“绑定”

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

文档中的jquery的版本是什么? –

+0

我有1.7.1的版本 –

+0

你使用的是什么版本的jQuery? .on()方法在jQuery 1.7中添加。 – Flea777

回答

2

问题是error事件不会冒泡,因此您无法使用事件委托在document级别捕获它。

捕获它的唯一方法是直接绑定到元素。


您可以通过使用event对象的.bubbles财产在你.bind()处理程序确定这一点。

$("#someid img").bind("error", function(event) { 
    console.log(event.bubbles); // false 
}); 
+0

好吧,绑定在绑定事件后添加到dom上的html不起作用。 –

+0

@MuditTuli:对。您将不得不在创建元素时直接绑定元素。 – user2736012

0
$("#your_id").error(function() { 
    alert("your text") 
}) 
的同类作品
  1. 。在jquery1.7之后,on()代替.bind(), .live() and .delegate()
  2. .on()管理事件委托,绑定不。
  3. 什么是event.delegation