How to get the innerHtml, including the tag, using jQuery?

后端 未结 4 574
心在旅途
心在旅途 2021-01-02 06:45

Sorry, if the title is too obscure ;D.

Actually the problem is, lets say i am this code.


   

        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-02 07:16

    This will create a new div and append a clone of your element to that div. The new div never gets inserted into the DOM, so it doesn't affect your page.

    var theResult = $('
    ').append($("#spanIDxx").clone()).html(); alert( theResult );

    If you need to use this frequently, and don't want to bother with adding yet another plugin, just make it into a function:

    function htmlInclusive(elem) { return $('
    ').append($(elem).clone()).html(); } alert( htmlInclusive("#spanIDxx") );

    Or extend jQuery yourself:

    $.fn.htmlInclusive = function() { return $('
    ').append($(this).clone()).html(); } alert( $("#spanIDxx").htmlInclusive() );

提交回复
热议问题