浙江省高等学校教师教育理论培训

微信搜索“教师资格证岗前培训”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

让 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.

posted on 2010-12-08 12:20  lexus  阅读(1699)  评论(1编辑  收藏  举报