jQuery/javascript replace tag type

后端 未结 8 1541
抹茶落季
抹茶落季 2020-12-02 23:42

Is there an easy way to loop through all td tags and change them to th? (etc).

My current approach would be to wrap them with the th and then remove the td, but then

8条回答
  •  抹茶落季
    2020-12-03 00:02

    This is a bit cleaner than @GlenCrawford's answer and additionally copies the children of the replaced element.

    $('td').each(function(){
        var newElem = $('', {html: $(this).html()});
        $.each(this.attributes, function() {
            newElem.attr(this.name, this.value);
        });
        $(this).replaceWith(newElem);
    });
    

提交回复
热议问题