jQuery convert DOM element to different type

后端 未结 3 910
自闭症患者
自闭症患者 2020-12-19 02:43

I need to convert a DOM element to a different type (as in HTML tag name, a to p in this case), but still retain all the original elements attribut

3条回答
  •  心在旅途
    2020-12-19 02:56

    while not a complete solution, the logic would basically be:

    Save your existing element:

    var oldElement = $(your selector here);
    

    create a new element and insert it just before or after your oldElement

    copy the attributes

      oldElement.attr().each(function(){
        copy old
        });
    

    better yet, here is an example of a plug-in that does just what you want:

    http://plugins.jquery.com/project/getAttributes

提交回复
热议问题