例如,我想定位容器不同级别的所有嵌套 img
元素。
<div>
<img>
<p><img></p>
<div>
<p>
<img>
</p>
</div>
</div>
此外,我想按大小过滤它们,因此我使用了 if (imgs.width() > 1080) {}
我正在使用 jQuery find()
方法,但没有任何错误或结果。这是我得到的
var imgs = $(".entry-content").find("img");
imgs.each(function() {
if (imgs.width() > 1080) {
imgs.css({"width": "100%", "height": "auto"});
}
});
最佳答案
我认为您根本不需要使用 find 。在 jQuery 中,您需要使用 $(this) 来定位该循环的特定实例。
$('.entry-content img').each(function (i, o) {
if ($(this).outerWidth() > 1080) {
$(this).css({"width": "100%", "height":"auto"});
}
});
关于javascript - 查找所有嵌套的 img 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35808075/