Get selected element's outer HTML

前端 未结 30 2350
礼貌的吻别
礼貌的吻别 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 04:59

    Pure JavaScript:

    var outerHTML = function(node) {
      var div = document.createElement("div");
      div.appendChild(node.cloneNode(true));
      return div.innerHTML;
    };
    
    0 讨论(0)
  • 2020-11-21 05:00

    You can find a good .outerHTML() option here https://github.com/darlesson/jquery-outerhtml.

    Unlike .html() that returns only the element's HTML content, this version of .outerHTML() returns the selected element and its HTML content or replaces it as .replaceWith() method but with the difference that allows the replacing HTML to be inherit by the chaining.

    Examples can also be seeing in the URL above.

    0 讨论(0)
  • 2020-11-21 05:00

    I have made this simple test with outerHTML being tokimon solution (without clone), and outerHTML2 being jessica solution (clone)

    console.time("outerHTML");
    for(i=0;i<1000;i++)
     {                 
      var html = $("<span style='padding:50px; margin:50px; display:block'><input type='text' title='test' /></span>").outerHTML();
     }                 
    console.timeEnd("outerHTML");
    
    console.time("outerHTML2");
    
     for(i=0;i<1000;i++)
     {                 
       var html = $("<span style='padding:50px; margin:50px; display:block'><input type='text' title='test' /></span>").outerHTML2();
      }                 
      console.timeEnd("outerHTML2");
    

    and the result in my chromium (Version 20.0.1132.57 (0)) browser was

    outerHTML: 81ms
    outerHTML2: 439ms

    but if we use tokimon solution without the native outerHTML function (which is now supported in probably almost every browser)

    we get

    outerHTML: 594ms
    outerHTML2: 332ms

    and there are gonna be more loops and elements in real world examples, so the perfect combination would be

    $.fn.outerHTML = function() 
    {
      $t = $(this);
      if( "outerHTML" in $t[0] ) return $t[0].outerHTML; 
      else return $t.clone().wrap('<p>').parent().html(); 
    }
    

    so clone method is actually faster than wrap/unwrap method
    (jquery 1.7.2)

    0 讨论(0)
  • 2020-11-21 05:01

    Note that Josh's solution only works for a single element.

    Arguably, "outer" HTML only really makes sense when you have a single element, but there are situations where it makes sense to take a list of HTML elements and turn them into markup.

    Extending Josh's solution, this one will handle multiple elements:

    (function($) {
      $.fn.outerHTML = function() {
        var $this = $(this);
        if ($this.length>1)
          return $.map($this, function(el){ return $(el).outerHTML(); }).join('');
        return $this.clone().wrap('<div/>').parent().html();
      }
    })(jQuery);
    

    Edit: another problem with Josh's solution fixed, see comment above.

    0 讨论(0)
  • 2020-11-21 05:01

    Short and sweet.

    [].reduce($('.x'), function(i,v) {return i+v.outerHTML}, '')
    

    or event more sweet with help of arrow functions

    [].reduce.call($('.x'), (i,v) => i+v.outerHTML, '')
    

    or without jQuery at all

    [].reduce.call(document.querySelectorAll('.x'), (i,v) => i+v.outerHTML, '')
    

    or if you don't like this approach, check that

    $('.x').get().reduce((i,v) => i+v.outerHTML, '')
    
    0 讨论(0)
  • 2020-11-21 05:01

    This is quite simple with vanilla JavaScript...

    document.querySelector('#selector')
    
    0 讨论(0)
提交回复
热议问题