Jquery 显示和隐藏悬停上最近的元素无效
我在一个页面中有多个元素,如下所示:Jquery 显示和隐藏悬停上最近的元素无效,jquery,Jquery,我在一个页面中有多个元素,如下所示: <div> <span class="content_video_wrapper_video_icon"> <i class="glyphicon glyphicon-facetime-video"></i> </span> <span class="content_video_wrapper_video
<div>
<span class="content_video_wrapper_video_icon">
<i class="glyphicon glyphicon-facetime-video"></i>
</span>
<span class="content_video_wrapper_video_text" style="display: none;">
<a href="...">Go to video</a>
</span>
</div>
<div>
<span class="content_video_wrapper_video_icon">
<i class="glyphicon glyphicon-facetime-video"></i>
</span>
<span class="content_video_wrapper_video_text" style="display: none;">
<a href="...">Go to video</a>
</span>
</div>
<div>
<span class="content_video_wrapper_video_icon">
<i class="glyphicon glyphicon-facetime-video"></i>
</span>
<span class="content_video_wrapper_video_text" style="display: none;">
<a href="...">Go to video</a>
</span>
</div>
我做错了什么?当
最近的
向上遍历时,尝试下面的代码更改:(获取最近的div,然后查找所需的元素)
最近的
向上遍历,而不是向下遍历,因此找不到文本项。图标和文本跨度是否互换有关系?@PaulT。我只是换了一下,试了试,没用。抱歉。您可能可以尝试原始代码,但在处理程序中更改为:$(this).closest('div')。find(“.content\u video\u wrapper\u video\u text”)。show()
应该可以。然后将同样的方法应用于隐藏。谢谢你,保罗,效果很好。也许你可以给我的帖子回复你的评论,这样我就可以选择它作为正确的答案了?
$(".content_video_wrapper_video_icon").each((i, el) => {
$(el).hover(
function() {
$(this).closest(".content_video_wrapper_video_text").show()
},
function() {
$(this).closest(".content_video_wrapper_video_text").hide()
}
)
})
$(".content_video_wrapper_video_icon").each((i, el) => {
$(el).hover(
function() {
// $(this).closest(".content_video_wrapper_video_text").show()
$(this).closest('div').find(".content_video_wrapper_video_text").show()
},
function() {
// $(this).closest(".content_video_wrapper_video_text").hide()
$(this).closest('div').find(".content_video_wrapper_video_text").hide()
}
)
})