Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery-单独处理每个img元素,而不使用类或父元素_Javascript_Jquery_Html_Image - Fatal编程技术网

Javascript jQuery-单独处理每个img元素,而不使用类或父元素

Javascript jQuery-单独处理每个img元素,而不使用类或父元素,javascript,jquery,html,image,Javascript,Jquery,Html,Image,我想做的是让src中的图像尺寸根据图像的width属性进行调整。(注意尺寸在url中定义,“s1600”为原始尺寸,“s320”为320px宽等) 我450多篇文章中的图像没有class属性 我用有限的jQuery知识成功地走到了这一步,但现在我陷入了困境。正如您所看到的,我的代码正在计算它在页面上找到的第一个图像,并将这些维度应用于每一个其他图像,从而导致其他img元素的分辨率不高 $('img').each(function(){ var $this = $(this) va

我想做的是让src中的图像尺寸根据图像的width属性进行调整。(注意尺寸在url中定义,“s1600”为原始尺寸,“s320”为320px宽等)

我450多篇文章中的图像没有class属性

我用有限的jQuery知识成功地走到了这一步,但现在我陷入了困境。正如您所看到的,我的代码正在计算它在页面上找到的第一个图像,并将这些维度应用于每一个其他图像,从而导致其他img元素的分辨率不高

$('img').each(function(){
    var $this = $(this)
    var imgWidth = $("img").width() + 100
  $this.attr('src',$this.attr('src').replace('s1600','s' + imgWidth))
}) 
jsIDLE:

此处:

HTML

<img width="200" src="http://3.bp.blogspot.com/-NX_ClFb2-Uk/UGJHjrprEAI/AAAAAAAAARk/HD7o4dbV7Wo/s1600/bookshelves-at-the-library_w482_h725.jpg" />
<br/>
<img width="640" src="http://3.bp.blogspot.com/-NX_ClFb2-Uk/UGJHjrprEAI/AAAAAAAAARk/HD7o4dbV7Wo/s1600/bookshelves-at-the-library_w482_h725.jpg" />

用$this.width()+100代替$(“img”).width(),怎么样?@LJ_1102谢谢。这就是我想做的!
$('img').each(function(){
    $(this).attr('src',$(this).attr('src').replace('s1600','s' + parseFloat($(this).css('width'))));
});