jquery 在 Firefox 中切换 dt 元素,但在其他浏览器中不起作用

发布于 2024-09-12 16:25:59 字数 626 浏览 5 评论 0原文

以下内容适用于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

[浮城] 2024-09-19 16:25:59

你的函数逻辑是错误的,但它在 Firefox 中工作,因为它处理你的 HTML 的方式与其他浏览器不同。

next() 函数查看相关元素的直接兄弟元素。您正在 h3 元素上执行此操作:

<dt><h3>stuff to be clicked</h3></dt>

如您所见,h3 标记没有直接同级元素。它没有同级(因此没有 next())。

那么为什么 Firefox 可以工作呢?因为你的HTML也是无效的。根据 DT 的规范dt 标签 标签仅采用inline 元素,而h3 标签不是内联标签。为什么这很重要?因为不同的浏览器对无效文档的处理方式不同。在这种情况下,Firefox 会剔除 h3 标记或 dt 标记,使您的文档结构如下所示:

<dt></dt>
<h3>stuff to be clicked</h3>
<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>

因此,在 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 the h3 element:

<dt><h3>stuff to be clicked</h3></dt>

As you can see, there are no immediate siblings to the h3 tag. It has no siblings (and thus no next()).

So why does firefox work? Because your HTML is also invalid. According to the specifications of the DT tag the dt tag takes only inline elements and the h3 tag is not an inline tag. Why does this matter? Because different browsers deal differently with invalid documents. Firefox, in this case, kicks out the h3 tag or the dt tag, leaving your document structure like this:

<dt></dt>
<h3>stuff to be clicked</h3>
<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>

So in Firefox' case, the h3 element actually becomes siblings with the dd tag, and your code works. However, in other browsers (such as Chrome, which is tested on), the h3 tag remains inside the dt tag and your code doesn't work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文