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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值