如何在列表项目中的锚点中添加类?
我正在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个错误:
- 我正在寻找一堂课,但当然应该在寻找ID。
- 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:
- I was looking for a class but of course should be looking for an id.
- 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:
- I was looking for a class but of course should be looking for an id.
- Wordpress is uses jQuery instead of $!
The working code is:
Thanks again, I am very happy!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
列表项目(或WAS)上缺少闭合,
将
fl-scroll-link
类添加到锚点中使用.addclass()
方法。将其应用于所有儿童&lt; a&gt;
在菜单下方的元素,如下所示。由于您已经在使用jQuery,因此我也使用过。另外,如果您想通过其锚固href值实际访问它,那么还有一个问题已经回答了这里:如何通过jQuery中的href获取元素?
在您的情况下:
关于“ $”的错误消息不确定,这意味着您的jQuery库未加载您的jQuery库。您需要确保在网站加载jQuery之后添加自定义JavaScript ...或以下是一种纯JavaScript的方法,可以将类添加到该
&lt; a&gt;
标签中。或者,如果您想将该类应用于列表项目下的所有
&lt; a&gt;
标签。There is a missing closure on your list item (or was)
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.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:
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.or if you wanted to apply that class to all
<a>
tags under the list item.