简体   繁体   中英

How to transform element to string with jQuery

I don't need innerHTML i need innerHTML with enclosing tags. Lets write some example:

<div id="1" style="qwe"><span class="1"></span></div>
<div id="2" style="asd"><span class="2"></span></div>
<div id="3" style="zxc"><span class="3"></span></div>

I can get element by id:

$("#1")

And how can i get string like that:

<div id="1" style="qwe"><span class="1"></span></div>

Of course html() doesn't work becouse it will return only span.

you could do something like this:

alert( $('#\\31 ').wrap("<div />").parent().html() )
$('#\\31 ').unwrap()

Something like this should work fine:

jQuery.fn.outerHTML = function(s) {
    return s
        ? this.before(s).remove()
        : jQuery("<p>").append(this.eq(0).clone()).html();
};

var outer = $("#1").outerHTML();

Here's a working fiddle .

Additional Info

See http://www.yelotofu.com/2008/08/jquery-outerhtml/ for original article .

Use this jQuery plugin: https://github.com/brandonaaron/jquery-outerhtml/blob/master/jquery.outerhtml.js

/*! Copyright (c) 2006 Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 */

(function($){
  var div;

  $.fn.outerHTML = function() {
    var elem = this[0],
      tmp;

    return !elem ? null
      : typeof ( tmp = elem.outerHTML ) === 'string' ? tmp
      : ( div = div || $('<div/>') ).html( this.eq(0).clone() ).html();
  };

})(jQuery);

Use it as follows:

$('#some-element').outerHTML();

You can use outerhtml but in JavaScript over the DOM and not jQuery , for example:

  var text = document.getElementById('hello').outerHTML;

jsbin code to demonstrate: http://jsbin.com/obutul/edit#javascript,html,live

html元素上还有一个outerHTML属性,它包含所选元素本身。

outerHTML is implemented across nearly all browsers now (including old versions of ie - firefox is the only one dragging its feet, but it's scheduled for v11 ), so I've adapted @James Hill's answer to use this native functionality where present as it should be more efficient.

jQuery.fn.outerHTML = function(s) {
    return this[0].outerHTML ? this[0].outerHTML :
           s ? this.before(s).remove()
             : jQuery("<p>").append(this.eq(0).clone()).html();
};

var outer = $("#1").outerHTML();

Be aware though that there are a few cross-browser inconsistencies in outerHTML (eg look at this page in chrome and compare with ie)

You can wrap the desired div in another div and then fetch the parent div's html.

<div><div id="1" style="qwe"><span class="1"></span></div></div>
<div><div id="2" style="asd"><span class="2"></span></div></div>
<div><div id="3" style="zxc"><span class="3"></span></div></div>

Now,

$("#1").parent().html() will fetch the desired string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM