jQuery/javascript replace tag type

后端 未结 8 1542
抹茶落季
抹茶落季 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:27

    $("td").each(function() {
      var tmp = $('
    ').append($(this).clone(true)).html().replace(/td/i,'th'); $(this).after(tmp).remove(); });

    or pure DOM

    function replaceElm(oldTagName, newTagName, targetElm) {
      var target = targetElm || window.document;
      var allFound = target.getElementsByTagName(oldTagName);
      for (var i=0; i

    DOM is always faster: http://jsperf.com/replace-tag-names

提交回复
热议问题