Jquery 向多个img标记的Src添加高度和宽度
我有以下html代码:Jquery 向多个img标记的Src添加高度和宽度,jquery,html,Jquery,Html,我有以下html代码: <p>adfa</p> <p><img src="http://localhost:27756//download.jpg" alt="" width="282" height="179" /></p> <p>adfa</p> <p><img src="http://localhost:27756/Summit 2013-5.jpg" alt="" width="358"
<p>adfa</p>
<p><img src="http://localhost:27756//download.jpg" alt="" width="282" height="179" /></p>
<p>adfa</p>
<p><img src="http://localhost:27756/Summit 2013-5.jpg" alt="" width="358" height="358" /></p>
<p>adfa</p>
adfa
adfa
adfa
现在我想将每个图像的高度和宽度添加到它的src中。我该怎么做?我对jQuery非常陌生。尝试以下方法:
$('img').each(function() {
alert($(this).attr('src'));
$(this).css('width',500);
$(this).css('height',500);
});
如果要添加到img
属性,可以执行以下操作:
$('img').each(function(i) {
alert('OLD:Width for image at index '+i+' is '+$(this).attr('width'));
alert('OLD:height for image at index '+i+' is '+$(this).attr('height'));
$(this).attr('width',500);
$(this).attr('height',500);
alert('NEW:Width for image at index '+i+' is '+$(this).attr('width'));
alert('NEW:height for image at index '+i+' is '+$(this).attr('height'));
});
演示小提琴:尝试以下方法:
$('img').each(function() {
alert($(this).attr('src'));
$(this).css('width',500);
$(this).css('height',500);
});
如果要添加到img
属性,可以执行以下操作:
$('img').each(function(i) {
alert('OLD:Width for image at index '+i+' is '+$(this).attr('width'));
alert('OLD:height for image at index '+i+' is '+$(this).attr('height'));
$(this).attr('width',500);
$(this).attr('height',500);
alert('NEW:Width for image at index '+i+' is '+$(this).attr('width'));
alert('NEW:height for image at index '+i+' is '+$(this).attr('height'));
});
演示小提琴:
函数addDimensionsToSrc(){
jQuery(“img”)。每个函数(idx、el){
el.src=el.src+“?height=“+el.height+”&width=“+el.width;
});
}
adfa
adfa
adfa
函数addDimensionsToSrc(){
jQuery(“img”)。每个函数(idx、el){
el.src=el.src+“?height=“+el.height+”&width=“+el.width;
});
}
adfa
adfa
adfa
在这里拉小提琴:
要使用真实图像的宽度和高度(因为这可能与属性设置不同),可以执行以下操作:
$("img").each(function(idx, e) {
$(this).attr("src", $(this).attr('src') + '?width=' + $(this).width() + '&height=' + $(this).height());
});
在这里拉小提琴:
要使用真实图像的宽度和高度(因为这可能与属性设置不同),可以执行以下操作:
$("img").each(function(idx, e) {
$(this).attr("src", $(this).attr('src') + '?width=' + $(this).width() + '&height=' + $(this).height());
});
用上面的代码创建一个html文件,在浏览器中运行,点击F12打开控制台,然后逐行运行javascript。用上面的代码创建一个html文件,在浏览器中运行,点击F12打开控制台,然后逐行运行javascript。我明白了。谢谢不客气,很乐意帮忙。如果你想使用真实的图像宽度和高度而不是属性所说的,你可以使用
$(this).width()
和$(this).height()
。我想到了。谢谢不客气,很乐意帮忙。如果要使用真实图像的宽度和高度而不是属性所说的,可以使用$(this).width()
和$(this).height()。