如何获取li下所以span的宽度和,然后赋值给ul宽度

例如上面获取 class="b" 的li下所有<span>标签的宽度和,然后赋值给 class="a"的ul宽度

jQuery

 $('ul.a').css('width',eval$('li.b span').map(function(){return this.offsetWidth}).get().join('+'))+'px')

<!DOCTYPE html>








  • asfasd
    asdfas
    asdf
    asdf
    asdf
    asdfasdfasd


<br> var wc=0;<br> var wcArr=[];<br> $(&quot;li.b&quot;).find(&#39;span&#39;).each(function(){<br> wc+=parseInt($(this).width());<br> wcArr.push(parseInt($(this).width()));<br> });<br> $(&#39;ul.a&#39;).width(wc);<br> console.log(wcArr);<br>

<ul class="a">
    <li class="b">
            <span>asfasd</span>
            <span>asdfas</span>
            <span>asdf</span>
            <span>asdf</span>
            <span>asdf</span>
            <span>asdfasdfasd</span>
    </li>
</ul>
<script type="text/javascript">
    var wc=0;
    var wcArr=[];
    $("li.b").find('span').each(function(){
        wc+=parseInt($(this).width());
        wcArr.push(parseInt($(this).width()));
      });
    $('ul.a').width(wc);
    console.log(wcArr);
</script>

学习下jquery里面有很多

https://developer.mozilla.org/zh-CN/docs/Web/API/Node/nodeType 看下MDN

$('ul.a').css('width',eval$('li.b span').map(function(){return this.offsetWidth}).get().join('+'))+'px')

var wc=0;
var wcArr=[];
$("li.b").find('span').each(function(){
wc+=parseInt($(this).width());
wcArr.push(parseInt($(this).width()));
});
$('ul.a').width(wc);
console.log(wcArr);