jquery 目标 html5 标记属性

发布于 2024-11-15 11:13:32 字数 685 浏览 5 评论 0原文

我尝试使用 jQuery 简单地添加一个 .alt 类来替换主 section 中的 article 块,如下所示:

html5:

<section id="content" role="main">

  <h1>Main Title</h1>

  <article id="post-1" class="post-1 post type-post">
    .. article content ..
  </article>

  <article id="post-2" class="post-2 post type-post">
    .. article content ..
  </article>

  <article id="post-3" class="post-3 post type-post">
    .. article content ..
  </article>

</section>

jQuery:

$('#content article:nth-child(odd)').addClass('alt');

我是不是错过了什么……?这当然应该有效吗?

I'm trying to use jQuery to simply add an .alt class to alternate article blocks within a main section as follows:

html5:

<section id="content" role="main">

  <h1>Main Title</h1>

  <article id="post-1" class="post-1 post type-post">
    .. article content ..
  </article>

  <article id="post-2" class="post-2 post type-post">
    .. article content ..
  </article>

  <article id="post-3" class="post-3 post type-post">
    .. article content ..
  </article>

</section>

jQuery:

$('#content article:nth-child(odd)').addClass('alt');

Am I missing something...? Surely this should work?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

阳光①夏 2024-11-22 11:13:32

上面的代码确实有效,我的 jQuery 文件中的其他内容抛出了问题。请参阅上面的评论。

The above code does work, other content in my jQuery file was throwing out problems. See the comments above.

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