开发者

get .html AND the container [duplicate]

开发者 https://www.devze.com 2023-02-28 09:27 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: jQuery - Get selected element's outer HTML
This question already has answers here: Closed 11 years ago.

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("&lt;p&gt;").append(this.eq(0).clone()).html();
}

you can then simply call:

$('#myTag').outerHTML()
0

精彩评论

暂无评论...
验证码 换一张
取 消