如何在列表项目中的锚点中添加类?

发布于 2025-01-31 08:54:37 字数 1109 浏览 1 评论 0原文

我正在WordPress构建一个网站,需要在列表项目中的锚点元素中添加类。列表项目具有唯一的ID。我需要添加的课程将使我在锚点链接上平滑滚动。

我不想更改主题,这就是为什么我想与JavaScript添加类或任何更好的技术。

这是我尝试过但不起作用的方法:

$('li#menu-item-318').find('a').addClass('fl-scroll-link');

我是我的控制台,我会收到此错误:

TypeError: $ is not a function. (In '$("#menu-item-318 a")', '$' is undefined) Error: {}

这是我网站上的特定代码:

<nav class="top-bar-nav"><ul id="menu-topbalk"><li id="menu-item-318"><a href="#contactgegevens" class="nav-link">Contact</a></li></ul></nav>

因此,上述标签需要获得第二类:fl-scroll-croll-link,以便这将是结果:

<a href="#contactgegevens" class="nav-link fl-scroll-link">Contact</a>

我每天都在学习,可以利用您的帮助。请解释您建议的解决方案。

//解决方案//

非常感谢大家,我们在一起工作!哇!

因此,我犯了2个错误:

  1. 我正在寻找一堂课,但当然应该在寻找ID。
  2. WordPress使用jQuery而不是$!

工作代码是:

jQuery(document).ready(function($) {
    $("#menu-item-318 a").addClass("fl-scroll-link");
});

再次感谢,我很高兴!

I am building a website in Wordpress and need to add a class to the anchor element that is inside a list item. The list item has a unique ID. The class I need to add will give me smooth scrolling on the anchor link.

I do not want to alter the theme, that is why I want to add a class with JavaScript, or any better technique.

This is what I have tried but does not work:

$('li#menu-item-318').find('a').addClass('fl-scroll-link');

I my Console I get this error:

TypeError: $ is not a function. (In '$("#menu-item-318 a")', '

This is the specific code on my website:

<nav class="top-bar-nav"><ul id="menu-topbalk"><li id="menu-item-318"><a href="#contactgegevens" class="nav-link">Contact</a></li></ul></nav>

So the above a tag needs to get this second class: fl-scroll-link so that this will be the result:

<a href="#contactgegevens" class="nav-link fl-scroll-link">Contact</a>

I am learning every day and could use your help. Please explain the solution you propose.

// Solution //

Thanks a lot everyone, together we made it work! Hooray!

So I made 2 mistakes:

  1. I was looking for a class but of course should be looking for an id.
  2. Wordpress is uses jQuery instead of $!

The working code is:

jQuery(document).ready(function($) {
    $("#menu-item-318 a").addClass("fl-scroll-link");
});

Thanks again, I am very happy!

is undefined) Error: {}

This is the specific code on my website:

So the above a tag needs to get this second class: fl-scroll-link so that this will be the result:

I am learning every day and could use your help. Please explain the solution you propose.

// Solution //

Thanks a lot everyone, together we made it work! Hooray!

So I made 2 mistakes:

  1. I was looking for a class but of course should be looking for an id.
  2. Wordpress is uses jQuery instead of $!

The working code is:

Thanks again, I am very happy!

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

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

发布评论

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

评论(1

音盲 2025-02-07 08:54:37

列表项目(或WAS)上缺少闭合,

<nav class="top-bar-nav">
  <ul id="menu-topbalk">
    <li id="menu-item-318">  <!-- missing ">" here -->
      <a href="#contactgegevens" class="nav-link">Contact</a>
    </li>
  </ul>
</nav>

fl-scroll-link类添加到锚点中使用.addclass()方法。将其应用于所有儿童&lt; a&gt;在菜单下方的元素,如下所示。由于您已经在使用jQuery,因此我也使用过。

jQuery(document).ready(function($) {
    $("#menu-item-318 a").addClass("fl-scroll-link");
});

另外,如果您想通过其锚固href值实际访问它,那么还有一个问题已经回答了这里:如何通过jQuery中的href获取元素?

在您的情况下:

jQuery(document).ready(function($) {
    $('a[href="#contactgegevens"]').addClass("fl-scroll-link");
});

关于“ $”的错误消息不确定,这意味着您的jQuery库未加载您的jQuery库。您需要确保在网站加载jQuery之后添加自定义JavaScript ...或以下是一种纯JavaScript的方法,可以将类添加到该&lt; a&gt;标签中。

document.querySelector('a[href="#contactgegevens"]').classList.add("fl-scroll-link");

或者,如果您想将该类应用于列表项目下的所有&lt; a&gt;标签。

document.querySelector('#menu-item-318 a').classList.add("fl-scroll-link");

There is a missing closure on your list item (or was)

<nav class="top-bar-nav">
  <ul id="menu-topbalk">
    <li id="menu-item-318">  <!-- missing ">" here -->
      <a href="#contactgegevens" class="nav-link">Contact</a>
    </li>
  </ul>
</nav>

To add the fl-scroll-link class to an anchor use the .addClass() method. Apply this to all of the child <a> elements underneath the menu like below. Since you're already using jquery I've used that also.

jQuery(document).ready(function($) {
    $("#menu-item-318 a").addClass("fl-scroll-link");
});

Also if you wanted to actually access it by its anchor href value there is another question that has answered that already here: How to get an element by its href in jquery?

In your case:

jQuery(document).ready(function($) {
    $('a[href="#contactgegevens"]').addClass("fl-scroll-link");
});

The error message about "$" being undefined means that your jquery library isn't being loaded. You'll want to make sure that your custom javascript is added AFTER the site loads jquery... or below is a pure javascript way to add a class to that <a> tag.

document.querySelector('a[href="#contactgegevens"]').classList.add("fl-scroll-link");

or if you wanted to apply that class to all <a> tags under the list item.

document.querySelector('#menu-item-318 a').classList.add("fl-scroll-link");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文