如何通过 javascript 添加带有属性值的 img 标签?
How to add an img tag with attribute values via javascript?
我需要知道如何创建 class 到 "field" 到 javascript 或 jquery 的图像标签。谢谢!
您可以使用createElement
var img = document.createElement('img');
img.className = 'field';
//set any other properties you want
//add it to the dom
或使用jQuery
var $img = $('img', {
'class': 'field' //you can add more properties
});
//Add it to the dom
var img = '<img src="my.jpg" class="field" />';
$('body').append(img);
我需要知道如何创建 class 到 "field" 到 javascript 或 jquery 的图像标签。谢谢!
您可以使用createElement
var img = document.createElement('img');
img.className = 'field';
//set any other properties you want
//add it to the dom
或使用jQuery
var $img = $('img', {
'class': 'field' //you can add more properties
});
//Add it to the dom
var img = '<img src="my.jpg" class="field" />';
$('body').append(img);