如何使用jquery隐藏父元素? [英] How do I hide a parent element using jquery?
问题描述
假设以下HTML:
<li class="fooli">
<a class="foo" href="javascript:foo(this);">anchor</a>
</li>
<li class="fooli">
<a class="foo" href="javascript:foo(this);">anchor</a>
</li>
和以下Javascript(使用jquery 1.3.2):
and the following Javascript (using jquery 1.3.2):
function foo(anchor) {
alert($(anchor).attr('href'));
}
我的目标是能够隐藏所单击的li,但我无法为其分配唯一的ID.因此,我想通过$(anchor).parent().hide()之类的方法来进行定位(即确定所点击的特定锚点).
My goal is to be able to hide the li that is clicked on, but I can't assign them unique ids. Thus, I want to do it positionally (i.e. identify the particular anchor clicked on) by something like $(anchor).parent().hide().
但是,上面的警报返回"undefined",因此我什至没有正确的jquery对象.
However, the alert above returns "undefined", so it's not obvious to me that I even have the right jquery object.
我如何弄清楚对象$(anchor)是什么?特别是,如何查看它具有什么属性,它具有什么类,它是什么HTML元素等等?
How do I figure out what object $(anchor) is? In particular, how do I see what attributes it has, what class it has, what HTML element it is, etc?
推荐答案
您不能这样做:
$(function() {
$("a.foo").click(function() {
$(this).parent().hide();
return false;
});
});
具有:
<li class="fooli"><a class="foo" href="#">anchor</a></li>
<li class="fooli"><a class="foo" href="#">anchor</a></li>
这篇关于如何使用jquery隐藏父元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!