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

后端 未结 4 575
心在旅途
心在旅途 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 = $('<div />').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 $('<div />').append($(elem).clone()).html(); }
    
    alert( htmlInclusive("#spanIDxx") );
    

    Or extend jQuery yourself:

    $.fn.htmlInclusive = function() { return $('<div />').append($(this).clone()).html(); }
    
    alert( $("#spanIDxx").htmlInclusive() );
    
    0 讨论(0)
  • 2021-01-02 07:18

    You can copy the node to a new empty node, ask for new .parent() and then its .html()

    0 讨论(0)
  • 2021-01-02 07:24

    Haven't used this myself but looks like it may be of use:

    http://yelotofu.com/2008/08/jquery-outerhtml/

    demo here: http://yelotofu.com/labs/jquery/snippets/outerhtml/demo.html

    0 讨论(0)
  • 2021-01-02 07:34

    Clone might be useful to you. I don't believe you actually need to do anything with the clone/s.

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