我正在尝试使用jQuery只转换img标签和div标签
<img class="wrap" src="xyz.com" alt="test" />
<div class="test"></div>使用
<div class="wrap" src="xyz.com" alt="test" />试着这样做
var $select = $(".wrap");
var $div = $(".test");
var attributes = $select.prop("attributes");
$.each(attributes, function() {
$div.attr(this.name, this.value);
});上面的代码是从img复制所有属性,并将其分配给div标记。我想在不分配给html的情况下实现同样的效果。简单地说,我想用div标签替换img标签。这有可能吗?
发布于 2018-03-01 21:40:09
试试这个:
var $img = $("img.wrap");
var $div = $("div");
var attributes = $img.prop("attributes");
// loop through <img> attributes and apply them on <div>
$.each(attributes, function() {
$div.attr(this.name, this.value);
});
$img.remove();
$("#someContainer").append($div);HTML:
<div id="someContainer">
<img class="wrap" ... />
</div>来源:jQuery: How to copy all the attributes of one element and apply them to another?
https://stackoverflow.com/questions/49050860
复制相似问题