Change node type

前端 未结 4 1639
广开言路
广开言路 2021-01-05 03:44

Using jQuery, is it possible to change all matched elements to a different type?

For example, can I select all a tags, $(\"a\"), and change

4条回答
  •  萌比男神i
    2021-01-05 04:16

    No, you can't actually change it, but you can replace them with a new element using the replaceWith() method:

    $("a").replaceWith("");
    

    If there are any attributes that you want to keep, you'll need to manually set them:

    $("a").replaceWith(function() {
        return $("", {
            class: this.className,
            value: this.innerHTML
        });
    });
    

提交回复
热议问题