jQuery获取每个图像宽度,然后为每个图像添加css宽度

时间:2013-02-18 17:12:47

标签: jquery width

我想知道如何在不同的li中获得每个图像宽度(多个宽度),然后通过jQuery将宽度返回到每个图像。

所有图像都有不同的宽度。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

$('img').each(function() {

  var width = $(this).width();

   // OR

  var width = this.clientWidth;

  // to set that width to image try

  $(this).attr('width', width);

   // according to comment: how to assign width to parent li

   $(this).parent('li').css('width', width + 'px');
});