jquery outerhtml

让 Firefox 支持 outerHTML (使用jQuery1.3)

Posted on 2009-06-16 17:43  吕霖 阅读(627)  评论(7)  编辑  收藏 

1、今天获取元素的html,而firefox却不支持如下代码

var elemstr = $("#" + name)[0].outerHTML;

2、看到网上很多文章讨论Firefox如何使用outerHTML,给出的解决方案都颇为复杂。

如果使用jQuery1.3,则问题变得简单多了!

使用如下代码,IE和FF均支持!

var elemstr = $("#" + name).parent().html();

希望本文能对你有所帮助!

 

wrong above

 

 

jQuery: outerHTML

The outerHTML property (IE only) could sometimes be very handy, especially if you're trying to replace an element entirely. Brandon Aaron has very kindly given us aouterHTML plugin that does half the job as it doesn't support replacements. The following code snippet fills in the blanks:

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

To get the outerHTML value of an element do this...

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

To replace #myTag entirely do this...

$('#myTag').outerHTML("<p>My brand new #myTag.</p>");

Hope this helps someone :)

Update: There's now a demo page.

你可能感兴趣的:(jquery)