最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - div nested inside a gives weird innerHTML result in FireFox - Stack Overflow

programmeradmin1浏览0评论

In IE 8, jQuery acts as I would expect:

$('div',$('<a><div></div></a>')).html('test').html()
"test"

In FireFox:

$('div',$('<a><div></div></a>')).html('test').html()
"<a>test</a>"

It puts anchors around what I wanted. Does anyone know why this would happen?

EDIT: Setting this with plain javascript (i.e. setting innerHTML) causes the problem. So I guess my real question is: why does firefox change what I set? Is this part of some esoteric specification, or is it a bug?

In IE 8, jQuery acts as I would expect:

$('div',$('<a><div></div></a>')).html('test').html()
"test"

In FireFox:

$('div',$('<a><div></div></a>')).html('test').html()
"<a>test</a>"

It puts anchors around what I wanted. Does anyone know why this would happen?

EDIT: Setting this with plain javascript (i.e. setting innerHTML) causes the problem. So I guess my real question is: why does firefox change what I set? Is this part of some esoteric specification, or is it a bug?

Share Improve this question edited Apr 23, 2012 at 15:02 Stian 1,2992 gold badges19 silver badges38 bronze badges asked Aug 5, 2010 at 17:22 XodarapXodarap 11.9k12 gold badges63 silver badges97 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

Wrapping an <a> around a <div> is invalid html. Maybe Firefox is fixing it for you on the fly and returning the valid html?

well, firefox maybe knows you're breaking the rules.

but it did not know you did if you use .append().

$('div',$('<a><div></div></a>')).html('').append('test').html(); // give you 'test'

Not sure if it applies here (as we dont know which doctype you're using) but wrapping an <a> around a <div> is perfectly valid in html5, but Firefox obviously isn't up to speed on that yet. I'm guessing FF4 will be though

Had the same issue with Firefox 3.6.18 (4.x, 5.x renders page correctly). Quick and dirty fix which I came up was to wrap everything inside <a> with <span>.

发布评论

评论列表(0)

  1. 暂无评论