我基本上理解JQuery,但我对它绝对是新手,我怀疑这很容易.

I've got my image src and id in a JSON response (converted to an object), and therefore the correct values in responseObject.imgurl and responseObject.imgid, and now I'd like to create an image with it and append it to a div (lets call it <div id="imagediv">. I'm a bit stuck on dynamically building the <img src="dynamic" id="dynamic"> - most of the examples I've seen involve replacing the src on an existing image, but I don't have an existing image.

推荐答案

In jQuery, a new element can be created by passing a HTML string to the constructor, as shown below:

var img = $('<img id="dynamic">'); //Equivalent: $(document.createElement('img'))
img.attr('src', responseObject.imgurl);
img.appendTo('#imagediv');

Jquery相关问答推荐

使用jquery ui来回滑动切换

Visual Studio - 将文本粘贴到 cshtml 中会删除文本

Twitter Bootstrap 是否包含 jQuery?

如何使用 jQuery 将表格的一行滚动到视图 (element.scrollintoView) 中?

数据表警告(表 id = 'example'):无法重新初始化数据表

JQUERY UI Accordion 开始折叠

如何在 JQuery DataTable 中默认显示所有行

Jquery,清除/清空 tbody 元素的所有内容?

字符串化(转换为 JSON)具有循环引用的 JavaScript 对象

如何在我的 WordPress 插件中包含 CSS 和 jQuery?

jQuery ajax 在 asp.net mvc 中上传文件

jQuery DataTables:延迟搜索直到输入 3 个字符或单击按钮

如果不是 jQuery,Javascript 中的美元符号是什么

订单对象是否由指定的 jQuery Select 器返回?

删除所有以某个字符串开头的类

fancybox2 / fancybox 导致页面跳转到顶部

jquery追加到列表的前面/顶部

如何判断值是否为 JSON 对象?

Jquery 日期 Select 器 z-index 问题

Bootstrap:在 Modal 中打开另一个 Modal