使用 jQuery 和 DOM 删除父级的父级
我的 html 看起来像这样:
<li>
<div>
<p class="delete">
<a href="#">X</a>
</p>
</div>
<div class="friend-avatar">
<img src="" />
</div>
</li>
单击“删除”段落中的锚标记后,我将显示一个弹出窗口(使用 jquery-alert),如果用户选择“是”,我希望包含此的整个 li 单击 a 淡出,然后删除它。我正在尝试这样的事情,但李仍然可见:
$(function() {
$(".delete a").click( function(){
jConfirm('Remove friend?', 'Confirmation Dialog',
function(r){
if(r==true){
parent_li = $(this).closest('li');
parent_li.fadeOut('slow', function() {$(this).remove();});
}
});
return false;
});
});
我做错了什么?
更新
刚刚注意到,当我单击此“删除”链接时,firebug 显示以下错误:
a.ownerDocument is undefined
/site_media/jquery/jquery-1.4.2.js
Line 117
My html looks like this :
<li>
<div>
<p class="delete">
<a href="#">X</a>
</p>
</div>
<div class="friend-avatar">
<img src="" />
</div>
</li>
After clicking anchor tag in 'delete' paragraph I'm showing a popup (using jquery-alert) and if user selects 'Yes' I would like the whole li containing this clicked a to fade out and then remove it. I was trying something like this, but the li stays visible :
$(function() {
$(".delete a").click( function(){
jConfirm('Remove friend?', 'Confirmation Dialog',
function(r){
if(r==true){
parent_li = $(this).closest('li');
parent_li.fadeOut('slow', function() {$(this).remove();});
}
});
return false;
});
});
What am I doing wrong ?
UPDATE
Just noticed, that when I click this 'delete' link, firebug shows following error :
a.ownerDocument is undefined
/site_media/jquery/jquery-1.4.2.js
Line 117
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不熟悉该插件,但我的猜测是
this
并不是指被单击的元素,而是指对话框。尝试在
jConfirm()
外部的变量中引用this
。I'm not familiar with that plugin, but my guess is that
this
does not refer to the element that was clicked, but rather to the dialog.Try referencing
this
in a variable outside thejConfirm()
.您应该将 $(this) 存储在变量中,因为弹出函数内的范围会发生变化。
做类似的事情:
You should store $(this) in a variable since the scope changes inside your popup function.
Do something like:
您对parent_li 的引用超出了变量的范围。
只是为了兴趣,我会将回调更改为它自己的函数,这样我就可以重用它。
Your reference to parent_li is outside the scope of the variable.
Just for interest's sake I would have changed the call back to its own function, that way I could reuse it.