javascript - 查找所有嵌套的 img 元素

标签 javascript jquery html

例如,我想定位容器不同级别的所有嵌套 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/

相关文章:

javascript - HTML 按钮没有消失?

javascript - onclick 事件在单击按钮时提交,但在按下键盘上的 Enter 时不提交?

php - 今天在 Wordpress 中的自定义帖子查询

php - 有没有办法在 Moodle 中生成 block 或创建类似嵌套block的样式?

javascript - 使用 jquery 按样式查找元素

javascript - 使用 Codeigniter 403 的 Ajax 请求(禁止)

javascript在数组开头推送元素

javascript - 连接列表的可排序滚动问题

javascript - 当数组太多时如何将值从一个数组分配给其他数组?

javascript - 返回下载文件的发布请求