javascript - 图像高度只会从大量图像中检索一次

标签 javascript jquery

就像我在标题中所写的那样,我有多个具有相同类的元素,我获取该类并尝试检查该图像的子图像的宽度/高度/src。

我只能获取第一个图像的高度//宽度,但是我获取了所有图像的 src。

这是 html:

<a class="test">
<img src="http://www.kitchenspro.com/files/Testimonials/Large/1502046700250312a71707.jpg">
</a>
<a class="test">
<img src="http://www.kitchenspro.com/files/Testimonials/Large/40183.9461166782.jpg">
</a>
<a class="test">
<img src="http://sphotos-a.xx.fbcdn.net/hphotos-ash4/s480x480/391362_365319466870994_1543740404_n.jpg">
</a>

这是jquery

var imagesrc= "";
var imageheight = "";
var imagewidth = "";
var i=0;
$(".test > img").each(function() {
i++;
imagesrc = $(this).attr("src");
imageheight = $(this).width();
imagewidth = $(this).height();
document.write("<br>This is image's src: " + imagesrc + " This is img height: " + imageheight + " this is width: " + imagewidth + "<br>");            
});

如果我没有以正确的方式提供代码,我深表歉意。

任何帮助将不胜感激。

提前致谢。

最佳答案

第一次调用 document.write 会破坏其余元素,因此您只能从其中一个元素获取信息,使用 .append() 或其他方法来显示结果。

关于javascript - 图像高度只会从大量图像中检索一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11514358/

相关文章:

javascript - 使用 angularjs 将复选框设置为选中时导致错误

javascript - onclick,如何隐藏 <div> 并显示其后面的内容?

javascript - 如何在 Javascript 中使用 replace() 替换多个字符串

javascript - 链接跳转到顶部和底部但平滑滚动不起作用

jquery - 不使用类来计算 div 数

javascript - jquery模板中的for循环?

用于检测旧版 Internet Explorer 并提供下载较新浏览器的 Javascript

javascript - JavaScript 中美元金额的正则表达式

javascript - 如何使用for循环在div中显示restful api获取的数据?

jQuery 不会显示我的输入值?