使用jquery的我的绑定方法有什么问题?为什么jquery.bind()在这种情况下不起作用?
工作
var Element = htmlBody.find('tag');
$(Element).bind('DOMSubtreeModified', function(event) {
console.log(Element[0]);
//code come here
});
不工作
var obj = {};
var tags = ['tag1', 'tag2'];
for (index in tags) {
obj[index] = htmlBody.find(tags[index]);
$(obj[index]).bind('DOMSubtreeModified', function(event) {
console.log(obj[index][0]);
// code doe not come here
});
}
您不应该使用'for ... in'来迭代数组。在你的情况下,你可能正在触及它的“长度”属性。改用计数器变量。 –
尝试使用$ .each()/ iterate使用临时变量 –
DOMSubtreeModified事件已被弃用。点击此处获得更多信息:http://stackoverflow.com/questions/6659662/why-is-the-domsubtree修饰-event-deprecated-in-dom-level-3 –