javascript - 如何使用jquery追加功能添加图像的src?

标签 javascript jquery html

使用jquery附加功能添加图像的src。我在 foreach 循环中使用变量,我想附加图像 src 。我怎么做。

我的jquery代码

$.each(data.products, function(key,val) {
  $("div#yourwishlist").append('<div class="col-sm-6 col-md-4"><article class="box"><figure><a class="hover-effect" title="" href="#"><img width="300" height="160" alt="" src="'+val('thumb')+'"></a></figure><div class="details"><a class="pull-right button uppercase" href="" title="View all">select</a><h4 class="box-title">'+val['name']+'</h4><label class="price-wrapper"><span class="price-per-unit">$170</span>avg/night</label></div></article></div>');
console.log(val['name']);
});

这段代码在我的情况下不起作用......

最佳答案

Hitesh,您可以向图像添加 src 属性,例如:

$.each(data.products, function(key,val) {
   $("div#yourwishlist").append('<img width="300" height="160" alt="" src="'+ val +'">');
});

// here val contains the path of image in it

关于javascript - 如何使用jquery追加功能添加图像的src?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38766877/

相关文章:

javascript - 在父框架和子框架之间共享 javascript include 文件

javascript - preventDefault 不适用于 tinyMCE keydown 事件

javascript - 通过 Javascript 压缩重复使用的 HTML(基于变量的变化)

javascript - 使用 JavaScript 在 UIWebView 中查找和自动填写 HTML 登录表单

javascript - Angular JS : Validate form fields before submit

javascript - 授权代码请求时 Google OAuth2 response_type 错误

javascript - HTML5 Canvas 不会渲染从 canvas.toDataURI() 接收的数据

jquery - 使用 jquery 识别特定元素的位置

jquery - HTML 5 音频播放速率和当前时间

html - fieldset 中的 <pre> 不遵守 "overflow: auto"指令