我有以下代码
<b class="xyzxterms" style="cursor: default; ">bryant keil bio</b>
如何将b
标签替换为h1
标签,但保留所有其他属性和信息?
我有以下代码
<b class="xyzxterms" style="cursor: default; ">bryant keil bio</b>
如何将b
标签替换为h1
标签,但保留所有其他属性和信息?
使用jQuery有一种方法:
var attrs = { };
$.each($("b")[0].attributes, function(idx, attr) {
attrs[attr.nodeName] = attr.nodeValue;
});
$("b").replaceWith(function () {
return $("<h1 />", attrs).append($(this).contents());
});
Example: http://jsfiddle.net/yapHk/
Update, here's a plugin:
(function($) {
$.fn.changeElementType = function(newType) {
var attrs = {};
$.each(this[0].attributes, function(idx, attr) {
attrs[attr.nodeName] = attr.nodeValue;
});
this.replaceWith(function() {
return $("<" + newType + "/>", attrs).append($(this).contents());
});
};
})(jQuery);
Example: http://jsfiddle.net/mmNNJ/