jQuery: how to change tag name?

前端 未结 17 949
無奈伤痛
無奈伤痛 2020-11-28 07:37

jQuery: how to change tag name?

For example:


    $1

I need

$1
<
17条回答
  •  伪装坚强ぢ
    2020-11-28 08:24

    Yet another script to change the node name

    function switchElement() {
      $element.each(function (index, oldElement) {
        let $newElement = $('<' + nodeName + '/>');
        _.each($element[0].attributes, function(attribute) {
          $newElement.attr(attribute.name, attribute.value);
        });
        $element.wrapInner($newElement).children().first().unwrap();
      });
    }
    

    http://jsfiddle.net/rc296owo/5/

    It will copy over the attributes and inner html into a new element and then replace the old one.

提交回复
热议问题