javascript - 如何使用特定 div 使用最接近的函数

标签 javascript jquery closest

一旦图像可见,我需要 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/

相关文章:

matlab - 如何找到接近给定值的值?

javascript - 火狐安卓 : how long does a webextensions background script last?

javascript - 将处理消息放在具有现有数据的数据表的表顶部?

javascript - 关注下一个 "tabindex"的元素,但在下一个 td 中

jquery - 在 .filter() 之后找到第 n 个 child

jquery - 删除最近的空 sibling

javascript - 设置自定义 HTML5 必填字段验证消息

javascript - 在输入字段字符串末尾添加特殊字符,jquery 自动添加逗号

javascript - 带有每张幻灯片转换/持续时间的 jQuery 幻灯片

jquery - append 到最近的 div 类(页面上具有相同类的多个 div)