繁体   English   中英

.appendTo在.bind(load)中不起作用-jQuery

[英].appendTo doesn't work in .bind(load) - jQuery

我在for循环中和.bind(“ load”)处理程序中使用.appendTo将图像添加到div中时遇到问题。 我尝试了很多事情,但没有任何效果,它只显示最后一张图片,而不显示三张图片。 代码非常简单,如下所示。

如果有人对我有暗示,那就太好了。 提前致谢!

 var $images = ["https://static.pexels.com/photos/769986/pexels-photo-769986.jpeg", "https://static.pexels.com/photos/767907/pexels-photo-767907.jpeg", "https://static.pexels.com/photos/746759/pexels-photo-746759.png"]; var i, figure, img; for (i = 0; i < $images.length; i += 1) { figure = $('<figure />'); img = $('<img />'); img.attr("src", $images[i]); img.unbind("load"); img.bind("load", function() { a = $('<a>', { href: this.src, itemprop: "contentUrl", 'data-size': this.width + "x" + this.height }); img.appendTo(a); a.appendTo(figure); figure.appendTo('#gallery-panel'); }); } 
 img { width: 10em; float: left; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <html> <body> <div id="gallery-panel"></div> </body> </html> 

更改:

img.appendTo(a);

至:

$(this).appendTo(a);

您是否遇到臭名昭著Javascript Loop问题? 因为所有的闭包都使用相同的img变量。

暂无
暂无

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

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