我需要从列表中获取最接近的类html,但如果直接放置closest
,它总是返回null。我尝试了其他一些想法,但它不起作用。
html:
<ul id="ulId">
<li class='effectLeg'>
Stuffs
</li>
<li class='test legColor' style=''></li>
<li class='effectLeg'>
Stuffs 2
</li>
<li class='test legColor'></li>
</ul>
所以,如果我将鼠标悬停在最后一个LI(对于exp。),则悬停将获得最接近的类effectLeg
并给我HTML或其他内容(在这种情况下为STUFFS 2)。
js :
$(".legColor").live("hover", function(){
alert($(this).closest("#ulId").find(".effectLeg").html());
});
我最接近的是使用find,但是find只获取了li
的第一个值
我做了一个exp:检查Demo
答案 0 :(得分:3)
.closest()
搜索元素的祖先,但看起来你正试图搜索兄弟姐妹。我想你只想要.prev()
:
$(".legColor").live("hover", function(){
console.log($(this).prev('li.effectLeg'));
});
答案 1 :(得分:1)
你的语法不正确......取决于你做之后最接近的事情:
alert($(this).closest("ul").html());
这将返回#ulId