Get selected element's outer HTML

前端 未结 30 3050
礼貌的吻别
礼貌的吻别 2020-11-21 04:50

I\'m trying to get the HTML of a selected object with jQuery. I am aware of the .html() function; the issue is that I need the HTML including the selected obje

30条回答
  •  没有蜡笔的小新
    2020-11-21 05:02

    I came across this while looking for an answer to my issue which was that I was trying to remove a table row then add it back in at the bottom of the table (because I was dynamically creating data rows but wanted to show an 'Add New Record' type row at the bottom).

    I had the same issue, in that it was returning the innerHtml so was missing the TR tags, which held the ID of that row and meant it was impossible to repeat the procedure.

    The answer I found was that the jquery remove() function actually returns the element, that it removes, as an object. So, to remove and re-add a row it was as simple as this...

    var a = $("#trRowToRemove").remove();            
    $('#tblMyTable').append(a);  
    

    If you're not removing the object but want to copy it somewhere else, use the clone() function instead.

提交回复
热议问题