这是 html:(示例)
<div class="lblTest well" data-src="001">Label 1</div>
to
<a class="lblTest well" data-src="001" href="#" data-toggle="tooltip" data-placement="bottom" title="Tooltip on bottom 1" >Label 1</a>
这是js:
$('.lblTest').each(function (i, n) {
var $element = $(this);
$element.replaceWith(function () {
return $('<a/>', {
html: this.innerHTML,
class: this.className,
href: '#',
'data-src':this.('dataSrc'), /// < ???
'data-toggle':'tooltip',
'data-placement':'bottom',
'title':'Tooltip on bottom ' + (i + 1)
});
});
});
此代码不正确:
'data-src':this.('dataSrc'), /// < ???
最佳答案
您的元素中没有 dataSrc
属性。应该是这样
$(this).data("src"),
关于javascript - 替换为不删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61090080/