替换元素并保留属性
以下几乎可以用lis替换span[data-type="yesno"]
所有实例,但我还想保留属性,类等。有没有办法以相同的方式inheritance属性作为HTML?
$('span[data-type="yesno"]').replaceWith(function(){ return $("", {html: $(this).html()}); })
你必须循环你的元素属性:
$('span[data-type="yesno"]').replaceWith(function(){ $li = $("", {html: $(this).html()}); $.each(this.attributes, function(i, attribute){ $li.attr(attribute.name, attribute.value); }); return $li; })