gpt4 book ai didi

javascript - 如何使用javascript for循环按类名获取元素宽度?

转载 作者:太空宇宙 更新时间:2023-11-04 15:54:20 26 4
gpt4 key购买 nike

当我测试时,它会提示 blank value30px我想获得警报 400px30px 我该怎么做?

.div_cover_threads_image{
width: 400px;
}
<div class="div_cover_threads_image">
test
</div>

<div class="div_cover_threads_image" style="width: 30px;">
test
</div>


<script>
var div_cover_threads_image_Elem = document.getElementsByClassName('div_cover_threads_image');
for(var i=0, len=div_cover_threads_image_Elem.length; i<len; i++)
{
alert(div_cover_threads_image_Elem[i].style.width);
}
</script>

最佳答案

当你标记 jQuery 时,这将返回元素的宽度,即使没有明确定义宽度也是如此:

.div_cover_threads_image{
width: 400px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div_cover_threads_image">
test
</div>

<div class="div_cover_threads_image" style="width: 30px;">
test
</div>


<script>
var div_cover_threads_image_Elem = document.getElementsByClassName('div_cover_threads_image');
for(var i=0, len=div_cover_threads_image_Elem.length; i<len; i++)
{
alert($(div_cover_threads_image_Elem[i]).width());
}
</script>

关于javascript - 如何使用javascript for循环按类名获取元素宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47149022/

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