替换元素并保留属性

ems*_*off 6 jquery dom replacewith

几乎以下工作在替换的所有实例span[data-type="yesno"]与LIS,但我想也保留属性,类等是否有结转以同样的方式作为HTML属性的方式吗?

$('span[data-type="yesno"]').replaceWith(function(){
    return $("<li>", {html: $(this).html()});
})
Run Code Online (Sandbox Code Playgroud)

Oli*_*erH 13

你必须循环你的元素属性:

$('span[data-type="yesno"]').replaceWith(function(){
   $li = $("<li>", {html: $(this).html()});
   $.each(this.attributes, function(i, attribute){
        $li.attr(attribute.name, attribute.value);
  });
  return $li;
})
Run Code Online (Sandbox Code Playgroud)