2016-03-04 109 views
0

我想針對所有嵌套img元素withing容器的不同程度,例如..找到所有嵌套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"}); 
    } 
    }); 

回答

1

我不相信你需要使用find。在jQuery中,您將希望使用$(this)來定位該循環的特定實例。

$('.entry-content img').each(function (i, o) { 
    if ($(this).outerWidth() > 1080) { 
    $(this).css({"width": "100%", "height":"auto"}); 
    } 
}); 
+0

這不會影響'.entry-content'容器的直接後代嗎? – Chris

+0

它會影響包含在'.entry-content'中的任何圖像,不管它有多深。 –