Move an element to another parent after changing its ID

后端 未结 1 999
南旧
南旧 2020-12-10 11:35

I have HTML like this:


    
    

        
相关标签:
1条回答
  • 2020-12-10 12:11

    It's certainly easy in jQuery:

    // jQuery 1.6+
    $("#photo").prop("id", "newId").appendTo("#someOtherDiv");
    
    // jQuery (all versions)
    $("#photo").attr("id", "newId").appendTo("#someOtherDiv");
    

    Working demo: http://jsfiddle.net/AndyE/a93Az/


    If you want to do it in plain ol' JS, it's still fairly simple:

    var photo = document.getElementById("photo");
    photo.id  = "newId";
    document.getElementById("someOtherDiv").appendChild(photo); 
    

    Working demo: http://jsfiddle.net/AndyE/a93Az/1/

    0 讨论(0)
提交回复
热议问题