<script src="jquery.js"></script>
<p id="a1">
<p class="aa" style="width: 200px;">423423423423423423423</p>
<p class="aa" style="width: 180px;">42342342342342342</p>
<p class="aa" style="width: 150px;">123123</p>
</p>
<script>
var w = 0
$("#a1 .aa").each(function(){
w += $("#a1 .aa").width();//获取宽度。并累加
})
$("#a1").width(w)
</script>
<style>
.aa{
display: inline-block;
}
</style>
Originally the result should be like this
But after removing the inline style, the result is great.
Why remove inline styles. That's it? ? ? ?
Shorten
style
标签对放到dom
和js
之前你的代码结构如果没有行内样式,会先把
.aa
按照块级元素
处理,宽度为100%js处理完成之后才读取style标签对,把
.aa
处理为行内块级元素
the widthThe synchronized code must be positioned correctly
w += $("#a1 .aa").width();//Get the width. And add up
Put the style in style at the top. The page is loaded from top to bottom. p is a block-level element. .aa{display:inlne-block;} does not work at the bottom
You don’t understand the true meaning of
$(selector)
Go and check what will be returned and you will know the problem
Another question
You need to understand where the style will be added and will it be loaded in advance?
What is the execution of script?