我正在尝试通过以下 jQuery 添加 img 标签:
var parentElement = $("#sidebar");
var img1 = $('<img />', {
id: 'headerImageLeft',
src: '/Images/Header/FGM.jpg',
class: 'img-responsive',
style: 'float: left;',
width: 80,
alt: ''
});
img1.appendTo(parentElement);
虽然这似乎工作正常,但我不确定这是添加类的正确方法。在 Visual Studio 2013 中,我收到一条设计时警告,提示我正在使用 future 的保留字,这是添加类的正确方法吗?
最佳答案
根据jQuery docs ,因为 class
是 reserved word ,需要引用。
Property names generally do not need to be quoted unless they are reserved words (as
class
is in this case).
所以:
var img1 = $('<img />', {
id: 'headerImageLeft',
src: '/Images/Header/FGM.jpg',
"class": 'img-responsive',
style: 'float: left;',
width: 80,
alt: ''
});
关于javascript - 使用 jQuery 添加图像标签 - 如何包含类名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33619194/