[英]get the closest value from a list jquery
我需要從我的列表中獲取最接近的類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());
});
最近我得到使用發現,但發現只有得到我的第一個值li
我做了一個exp:檢查演示
.closest()
搜索元素的祖先,但看起來你正試圖搜索兄弟姐妹。 我想你只想要.prev()
:
$(".legColor").live("hover", function(){
console.log($(this).prev('li.effectLeg'));
});
你的語法不正確......取決於你做之后你最接近的事情:
alert($(this).closest("ul").html());
這將返回#ulId
的html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.