简体   繁体   English

如何获得宽度是响应式或自动的元素的宽度

[英]how to get width of element that width is responsive or auto

i am try to get width of li by jquery 我试图通过jQuery获取li的宽度

i have list like 我有这样的清单

  <ul>
  <li>Home</li>
  <li>Advance Server and Server Confing</li>
  <li>FaQ</li>
  </ul> 

so try to get width of li and i am trying this 所以尝试获得li宽度,我正在尝试

$( "#main_menu ul li" ).each(function() {
     alert($("#main_menu ul li").width());  
});

it give me same width of each li . 它给我每个li相同的宽度。 i also try .outerWidth() function but it give me same width. 我也尝试.outerWidth()函数,但它给我相同的宽度。

  <ul>
  <li><span>Home</span></li>
  <li><span>Advance Server and Server Confing</span></li>
  <li><span>FaQ</span></li>
  </ul> 


$( "ul li span" ).each(function() {
    alert($(this).width());  
});

http://jsfiddle.net/48Apj/ http://jsfiddle.net/48Apj/

You are using each that's why you get the width for each of them. 您正在使用each ,这就是为什么要获得每个宽度的原因。

Try using it for a single one, and try out (this) which will ensure you get the result from the current element only. 尝试单独使用它,然后尝试一下(this) ,这将确保仅从当前元素获得结果。 You can use attr too. 您也可以使用attr

$('#main_menu ul li').click(function () {
  alert($(this).attr('id'));
}

Here is a fiddle: http://jsfiddle.net/afzaal_ahmad_zeeshan/t39Yk/ 这是一个小提琴: http : //jsfiddle.net/afzaal_ahmad_zeeshan/t39Yk/

For you, you can use width instead of id . 对于您来说,您可以使用width而不是id

http://jsfiddle.net/afzaal_ahmad_zeeshan/t39Yk/1/ (fiddle with width) http://jsfiddle.net/afzaal_ahmad_zeeshan/t39Yk/1/ (带有小提琴的宽度)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM