每个图像容器(图)需要采用其包含的图像(img)的宽度,并放置一个与容器相同宽度的(图例说明)。
如果我仅使用一个容器,则可以使用,但是当我使用多个图形时,它将使用第一个图像的大小设置每个图形。
因此,我在foreach循环中犯了一个逻辑错误,但找不到解决方案。有什么帮助吗?我在jQuery中很新。
<figure><img><figcaption></figcaption></figure>
<figure><img><figcaption></figcaption></figure>
<figure><img><figcaption></figcaption></figure>
<script>
$(document).ready(function(){
//make figure the width of its image child
$('figure').each(function() {
var childWidth = $('figure img').width();
console.log(childWidth);
$('figure').width(childWidth);
})
});
<script>
你是选择和设置所有figure
的元素width
中.each()
。代替
var childWidth = $("img", this).width(); // selects current `figure` `img`
为了
var childWidth = $('figure img').width(); // selects all `img` elements
和
$(this).width(childWidth); // selects current `figure`
为了
$('figure').width(childWidth); // selects all `figure` elements
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句