一旦图像可见,我需要 jquery 来获取最接近的 id。 这是我到目前为止的查询
(...)
if(isonscreen)
{
//this works, but I need it to find only images inside the content-bubble div tag
// and of course this grabs any image no matter what div the image is inside of
console.log($(this).closest('img').attr('id'));
}
(...)
<div class="content-bubble">
<h2>{{imageTitle}}</h2>
<img src="loading.gif" id="lazyload{{imgId}}" class="content-bubble-img"">
</div>
我已经尝试过这个,但它不起作用并返回未定义
console.log($(this).closest('.content-bubble-img').find('img').attr('id'));
console.log($('.content-bubble-img').closest('img').attr('id'));
最佳答案
我想你想要的是函数find()
,而不是closest()
。
关于javascript - 如何使用特定 div 使用最接近的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23560305/