javascript jQuery addClass() / removeClass() 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18448014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:57:55  来源:igfitidea点击:

jQuery addClass() / removeClass() not working

javascriptjquery

提问by fusi0n

<ul>
    <li class="selected"></li>
    <li></li>
    <li></li>
</ul>

That is my html. And the selectors I am using right now are:

那是我的html。我现在使用的选择器是:

    $("ul li:eq(0)").removeClass('selected');
    $("ul li:eq(1)").addClass('selected');

Problem is that I can not add or remove the 'selected' class from these elements. The html and jquery are both pretty straight-forward and as many examples as I could find did everything the same way, including official jQuery docs. (The script is loaded before the body tag, if it matters)

问题是我无法从这些元素中添加或删除“选定”类。html 和 jquery 都非常简单,并且我能找到的尽可能多的示例都以相同的方式完成所有操作,包括官方 jQuery 文档。(脚本在 body 标签之前加载,如果重要的话)

Why can't I remove and add the classes like this?

为什么我不能像这样删除和添加类?

The entire html page here

整个html页面在这里

回答by Ahmed Alaa El-Din

probably because you forgot to add $(document).ready(),also make sure you added the jquery.min.js file

可能是因为你忘记添加了$(document).ready(),还要确保你添加了 jquery.min.js 文件

$(document).ready( function () {
    $("ul li:eq(0)").removeClass('selected');
    $("ul li:eq(1)").addClass('selected');
});

回答by Aaron Digulla

The code looks correct, the problem must be elsewhere. How to debug this:

代码看起来是正确的,问题一定出在别处。如何调试:

  1. Look at how many elements the selector matches:

    console.log($("ul li:eq(1)").length);
    

    If you see 0, then the selector didn't match anything.

  2. Which element did match?

    console.log($("ul li:eq(1)")[0]);
    

    Most browsers allow to examine the element in the console.

  1. 查看选择器匹配了多少元素:

    console.log($("ul li:eq(1)").length);
    

    如果您看到 0,则选择器没有匹配任何内容。

  2. 哪个元素匹配?

    console.log($("ul li:eq(1)")[0]);
    

    大多数浏览器允许在控制台中检查元素。

A typical problem is that your code is executed before the elements exist. In the simple case, you just need to run your code after the DOM is ready:

一个典型的问题是您的代码在元素存在之前执行。在简单的情况下,您只需要在 DOM 准备好后运行您的代码:

$(function () {
    ...DOM is now ready...
});

But sometimes, other scripts will create elements. If that's the case for you, you'll need to tell us which frameworks and scripts you use to build / manipulate the DOM.

但有时,其他脚本会创建元素。如果是这种情况,您需要告诉我们您使用哪些框架和脚本来构建/操作 DOM。

Related:

有关的:

回答by Orlando

$("ul li:eq(0)").removeClass('selected');
$("ul li:eq(1)").addClass('selected');

works for me (http://jsfiddle.net/orlando/rhtqA/)

对我来说有效(http://jsfiddle.net/orlando/rhtqA/

but yeah you need to wait till DOM is ready to do stuff with jquery

但是是的,你需要等到 DOM 准备好用 jquery 做事

$(function () {
    $("ul li:eq(0)").removeClass('selected');
    $("ul li:eq(1)").addClass('selected');
});