Replace element and preserve attributes

后端 未结 1 891
耶瑟儿~
耶瑟儿~ 2021-01-18 12:23

The following almost works in replace all instances of span[data-type=\"yesno\"] with lis, but I\'d like to also preserve the attributes, classes, etc. Is there

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-18 12:47

    You have to loop on your element attributes :

    $('span[data-type="yesno"]').replaceWith(function(){
       $li = $("
  • ", {html: $(this).html()}); $.each(this.attributes, function(i, attribute){ $li.attr(attribute.name, attribute.value); }); return $li; })
  • 0 讨论(0)
提交回复
热议问题