jquery 在 Firefox 中切换 dt 元素,但在其他浏览器中不起作用
以下内容适用于 Firefox,但不适用于其他浏览器。
在不同浏览器中的父子关系是否不同?$('dd').parent().find('h3').toggle(
function(){
$(this).next('dd').slideDown(500);
},
function(){
$(this).next('dd').slideUp(500);
}
);
HTML 看起来像:
<dt><h3>stuff to be clicked</h3></dt>
<dd><p>stuff in dd might look like this with internal elements</p>
<ul>
<li>stuff1</li>
<li>stuff2 </li>
<li>stuff3 </li>
</ul>
</dd>
The following works in Firefox but in no other browser. Is the parent child relationship of <dl>
s different in the different browsers?
$('dd').parent().find('h3').toggle(
function(){
$(this).next('dd').slideDown(500);
},
function(){
$(this).next('dd').slideUp(500);
}
);
HTML looks like:
<dt><h3>stuff to be clicked</h3></dt>
<dd><p>stuff in dd might look like this with internal elements</p>
<ul>
<li>stuff1</li>
<li>stuff2 </li>
<li>stuff3 </li>
</ul>
</dd>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的函数逻辑是错误的,但它在 Firefox 中工作,因为它处理你的 HTML 的方式与其他浏览器不同。
next()
函数查看相关元素的直接兄弟元素。您正在h3
元素上执行此操作:如您所见,
h3
标记没有直接同级元素。它没有同级(因此没有next()
)。那么为什么 Firefox 可以工作呢?因为你的HTML也是无效的。根据
DT
的规范dt
标签 标签仅采用inline
元素,而h3
标签不是内联标签。为什么这很重要?因为不同的浏览器对无效文档的处理方式不同。在这种情况下,Firefox 会剔除h3
标记或dt
标记,使您的文档结构如下所示:因此,在 Firefox 的情况下,
h3 元素实际上与
dd
标签成为同级元素,并且您的代码可以正常工作。但是,在其他浏览器(例如经过测试的 Chrome)中,h3
标记保留在dt
标记内,因此您的代码无法正常工作。Your function logic is wrong, but it's working in firefox since it deals with your HTML different than other browsers.
The
next()
function looks at the immediate sibling of the element in question. You are doing this on theh3
element:As you can see, there are no immediate siblings to the
h3
tag. It has no siblings (and thus nonext()
).So why does firefox work? Because your HTML is also invalid. According to the specifications of the
DT
tag thedt
tag takes onlyinline
elements and theh3
tag is not an inline tag. Why does this matter? Because different browsers deal differently with invalid documents. Firefox, in this case, kicks out theh3
tag or thedt
tag, leaving your document structure like this:So in Firefox' case, the
h3
element actually becomes siblings with thedd
tag, and your code works. However, in other browsers (such as Chrome, which is tested on), theh3
tag remains inside thedt
tag and your code doesn't work.