admin管理员组文章数量:1401613
When i tested it's will be alert blank value
and 30px
I want to get alert 400px
and 30px
How can i do ?
.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>
When i tested it's will be alert blank value
and 30px
I want to get alert 400px
and 30px
How can i do ?
.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>
Share
Improve this question
asked Nov 7, 2017 at 2:26
mamiwmamiw
1234 silver badges9 bronze badges
2
- Possible duplicate of How do you read CSS rule values with JavaScript? – random_user_name Commented Nov 7, 2017 at 2:30
-
You've created some confusion by tagging
jQuery
but using vanillajavascript
in your snippet. Which do you want? – random_user_name Commented Nov 7, 2017 at 2:31
3 Answers
Reset to default 3As you've tagged jQuery, this will return the elements width even if no width was explicitly defined:
.div_cover_threads_image{
width: 400px;
}
<script src="https://ajax.googleapis./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>
You used Element.style
on the second div
which means inline style. For the first one you need to use window.getComputedStyle(Element,[, pseudoElt])
in order to get all styles of it.
Extract the width by:
var width=window.getComputedStyle(Element,null).getPropertyValue('width');
The returned value will be something like 400px
. If you wish to use the numeric value without unit, try parseFloat
.
var els = document.getElementsByClassName( 'div_cover_threads_image' );
for(var i=0, len=els.length; i<len; i++)
{
var puted = getComputedStyle( els[i], null );
alert( puted.getPropertyValue( 'width' ) );
}
If you use jQuery, things will be more simple. Just $('.div_cover_threads_image').width();
From jquery,
.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>
$( ".div_cover_threads_image" ).each(function() {
alert($( this ).width());
});
</script>
本文标签: jqueryHow to get elements width by classname using javascript for loopStack Overflow
版权声明:本文标题:jquery - How to get elements width by classname using javascript for loop? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744275302a2598382.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论