Possible Duplicate:
jQuery - Get selected element's outer HTML
so I have a div..
<div id="fred">
it has stuff in it
<div id="fred">
<tr id="thing1">hello1</tr>
<tr id="thing2">hello2</tr&g开发者_开发百科t;
</div>
if I call
$("#fred").html();
I get just the items inside:
<tr id="thing1">hello1</tr>
<tr id="thing2">hello2</tr>
I want the whole thing:
<div id="fred">
<tr id="thing1">hello1</tr>
<tr id="thing2">hello2</tr>
</div>
I know that I can ref using this[0], but it seems like there should be an easier way..
I tried .andSelf()
but apparently that doesn't do what I thought it should doo..
jQuery doesn't support outerHTML
: http://api.jquery.com/html/#comment-84945158
(.andSelf
is for element sets, not for depth.)
I think the best solution would be el[0].outerHTML
(no extra 'plugin' required and native JS s always good)
Get selected element's outer HTML
EDIT: I'll elaborate...
This plugin does the job:
jQuery.fn.outerHTML = function(s) {
return (s)
? this.before(s).remove()
: jQuery("<p>").append(this.eq(0).clone()).html();
}
you can then simply call:
$('#myTag').outerHTML()
精彩评论