<div class="videoItem">
<div class="innerVideoItem">
<a><div class="overlayBg"></div></a>
<a><img class="overlayPlay"><img></a>
</div>
</div>
<script>
$(".overlayPlay").hover(function(){
// How do I find overlayBg here?
});
</script>
如何在不使用$(".overlayBg")
的情况下访问.overlayBg?
答案 0 :(得分:5)
在您的标记overlayBg
和overlayPlay
不是兄弟姐妹,他们是表兄弟,您可以使用closest
和find
方法。
$(".overlayPlay").hover(function(){
$(this).closest('.innerVideoItem').find('.overlayBg');
// $(this).parent().prev().find('.overlayBg')
});
答案 1 :(得分:1)
在原生javascript中,获取前一个元素的方法是(在你的事件处理程序中):
this.parentElement.previousElementSibling.firstElementChild
答案 2 :(得分:1)
$(".overlayPlay").hover(function(){
$(this).parents("div.innerVideoItem").find("div.overlayBg");
});