我正在努力寻找用另一个属性替换我的属性的解决方案。
我的 HTML 是:
<a href="#" external>link</a>
我想用 target="_blank" 替换“external”
可行吗?
最佳答案
你不需要 jQuery。
只需使用选择器 a[external]
遍历具有 external
属性的 anchor 元素,然后删除 external
属性并设置 target
属性:
Array.prototype.forEach.call(document.querySelectorAll('a[external]'), function(a) {
a.removeAttribute('external');
a.setAttribute('target', '_blank');
});
就其值(value)而言,如果您想要 jQuery 替代方案,请使用:
$('a[external]').each(function () {
$(this).removeAttr('external');
$(this).attr('target', '_blank');
});
关于javascript - 替换标签内的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29523029/