$(this).parent().remove();在IE中无法正常工作 [英] $(this).parent().remove(); not working in IE
问题描述
任何想法为何下面的代码在IE中都无法使用,而在FF和Chrome中却可以:
Any ideas why the code below wouldn't work in IE, yet in FF and Chrome it does:
$(".remove", document.getElementById("ccusers")).live("click",
function () {
$(this).parent().remove();
});
如果我尝试:
alert($(this).parent().attr("id"));
我在FF& Chrome,而不是IE.
I get the id alerted out in FF & Chrome but not IE.
任何想法都需要做些什么?
Any ideas what needs to be done differently?
推荐答案
$(".remove, #ccusers").bind("click", function () {
$(this).parent().remove();
});
要确保您要删除树及其子级中的正确父级元素,可以尝试以下操作:
To make shure you are removing the right parent element up in the tree and its children you can try this:
$(this).parents('theParentYouWantToRemove').remove();
您可能会发现这很有用: jQuery中的bind和live方法之间有什么区别?
You may find this useful: What is the difference between the bind and live methods in jQuery?
P.S.我建议您仅使用$('.remove')
,因为我看不到为什么要使用$('.remove, #ccusers')
,因为这实际上意味着:
elements .remove
或element #ccusers
,
P.S. I suggest you to use only $('.remove')
, as I don't see any reason why you are doing $('.remove, #ccusers')
, as it will actually mean:
elements.remove
OR element #ccusers
,
并执行以下操作:$('.element' , '#element')
(请看引号!)您的第二"元素将执行 完全没有 ! (就像这里建议的那样).
And by doing: $('.element' , '#element')
(look at the quotes!) your 'second' element will do absolutely NOTHING ! (like many here suggested).
所以我唯一有效的建议是:
$(".remove").bind("click", function () {
$(this).parents('#ccusers').remove(); // or .parent() as I explained above
});
演示jsBin
(下载并在IE中尝试)
DEMO jsBin
(download and try in IE)
这篇关于$(this).parent().remove();在IE中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!