【发布时间】:2009-02-21 12:52:54
【问题描述】:
href标签发生点击事件,需要引用9级以上的a类,
所以:
<div class="blah" id="234">
<div>
<div>
<div>
<div>
<div>
<div><a href=""></a>
我知道的唯一方法是调用 parent() 9 次,我还能做什么?
【问题讨论】:
href标签发生点击事件,需要引用9级以上的a类,
所以:
<div class="blah" id="234">
<div>
<div>
<div>
<div>
<div>
<div><a href=""></a>
我知道的唯一方法是调用 parent() 9 次,我还能做什么?
【问题讨论】:
您可以通过父函数提供选择器
$(a).click(function() {
var theIdIs = $(this).parents("div.blah").attr("id");
});
【讨论】:
此外,如果您使用 jQuery 1.3+,您可以使用 closest 方法
$('a').click(function() {
if ($(this).closest("div.blah").hasClass("blah"))
{
// Do something with it
}
});
【讨论】: