Print out jQuery object as HTML

后端 未结 3 1569
再見小時候
再見小時候 2020-12-29 23:20

Is there a good way of printing out a jQuery object as pure HTML?

ex:





        
相关标签:
3条回答
  • 2020-12-29 23:36

    $('img')[0].outerHTML

    will give you the HTML of the first img on the page. You will then need to use a for loop to get the HTML of all the images, or put an ID on the image and specify it only in the jQuery selector.

    0 讨论(0)
  • 2020-12-29 23:46

    If you need to print the object in HTML format, use this extension outerHTML or this outerHTML.

    Update

    Update the link and include the code for second link:

    $.fn.outerHTML = function(){
    
        // IE, Chrome & Safari will comply with the non-standard outerHTML, all others (FF) will have a fall-back for cloning
        return (!this.length) ? this : (this[0].outerHTML || (
          function(el){
              var div = document.createElement('div');
              div.appendChild(el.cloneNode(true));
              var contents = div.innerHTML;
              div = null;
              return contents;
        })(this[0]));
    
    }
    
    0 讨论(0)
  • 2020-12-29 23:53

    You could wrap it and then use html:

    var img = $('img').eq(0);
    console.log(img.wrap("<span></span>").parent().html());
    
    0 讨论(0)
提交回复
热议问题