【发布时间】:2016-05-05 21:24:41
【问题描述】:
尝试使用 jQuery 更改项目的类,但我似乎无法使其工作。出于某种原因,什么都没有发生。
代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$("#theSpinner").click(function() {
$("#theSpinner").toggleClass("spinning");
});
</script>
<a href="#" id="reload">
<img src="spinbut.png" style="width:100px;height:100px;" id="theSpinner" class="notspinning">
</a>
我试过了:
$("#theSpinner").click(function() {
$("#theSpinner").removeClass("notspinning");
$("#theSpinner").addClass("spinning");
});
还有$("#reload") 而不是$("#theSpinner"),并带有onClick="" 到包含addClass / removeClass 部分的javascript 函数。
我真的不知道问题出在哪里,但是任何绕过这头公牛的方法都会让我很高兴。
【问题讨论】:
-
作为附加注释,您应该使用
this而不是点击函数内的相同选择器... -
@DarkAjax 我是!它在原始代码中,我只是变得绝望。不过谢谢!
-
您的代码运行良好。您希望看到什么?
-
@Gerard 显然我需要确保它已被加载,仅此而已(请参阅下面的答案)。 =)
标签: javascript jquery