gpt4 book ai didi

javascript - 如何动态给div的高度和宽度?

转载 作者:太空宇宙 更新时间:2023-11-04 00:33:32 24 4
gpt4 key购买 nike

使用 jQuery 为图像的父 div 提供高度和宽度的最佳方法是什么。

<div><img src="source.jpg" width="%width" height="%height" /></div>

请注意,这样的标签不止一个。 3-4 张图像和 DIVS。

提前致谢。

jQuery 解决方案将是首选。

最佳答案

使用 jQuery 维度。

为每张图片添加一个类:

<div><img class='myImage' src="source.jpg" width="%width" height="%height" /></div>

js:

$(".myImage").each(function(){
$(this).parent().css( { width: $(this).width(), height: $(this).height() } );
});

就是这样。

GL

关于javascript - 如何动态给div的高度和宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1559648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com