如果某个元素具有我正在使用 jQuery 查找的类,如何删除该类?
<a href='1.html' onclick='return false' class='selected'>video 1</a>
<a href='2.html' onclick='return false' class=''>video 2</a>
<a href='3.html' onclick='return false' class=''>video 3</a>
<a href='4.html' onclick='return false' class=''>video 4</a>
在上面的代码中,我有 4 个可能的菜单,根据我单击的链接标签而显示。每当我单击未选择且没有“选定”类的链接时,我想从先前选择的视频链接标记中删除“选定”类,并将该类添加到正在单击的链接中。
将类添加到正在单击的链接中没有问题,但是当我单击不同的链接时,如何从当前选定的链接标记中删除“选定”类?
我尝试了以下方法,但它不起作用
$('a').hasClass('selected').removeClass('selected');
,但显然它不是一个正确的函数,并且在 Firefox 的错误日志中给了我一条错误消息。
<a href='1.html' onclick='return false' class='selected'>video 1</a>
<a href='2.html' onclick='return false' class=''>video 2</a>
<a href='3.html' onclick='return false' class=''>video 3</a>
<a href='4.html' onclick='return false' class=''>video 4</a>
In the above code, I have 4 possible menus that appear depending on which link tag I click. Whenever I click on a link that is not selected and doesn't have the 'selected' class, I want to remove the 'selected' class from the previously selected video link tag, and add that class to the link that is being clicked on.
I have no problem adding the class to the link that is being clicked on, but how do I remove the class 'selected' from the currently selected link tag when I click on a different link?
I tried the following and it did not work
$('a').hasClass('selected').removeClass('selected');
but apparently it is not a proper function and gives me an error message in the error log in firefox.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
.hasClass()
返回一个布尔值。适当地使用选择器 API:也就是说,这在逻辑上是等效的:
.hasClass()
returns a boolean. Just use the selector API appropriately:That said, this is logically equivalent: