我有一个网站,其中html标记使用css具有背景图像。我创建了一个标签,当您单击它时,html标签的背景图片应通过CSS更改。我正在使用switchClass,但它不起作用。在我的代码下面找到:
线:
<html class="background1">
与触发jQuery的链接:
<div id=photoNum>
<a class="photoLink" href="#">1</a>
</div>
CSS:
.background1 {
background: url(Photos/Main.jpg) no-repeat center center fixed;
}
.background2 {
background: url(Photos/Main2.jpg) no-repeat center center fixed;
}
.background3 {
background: url(Photos/Main3.jpg) no-repeat center center fixed;
}
.background4 {
background: url(Photos/Main4.jpg) no-repeat center center fixed;
}
jQuery脚本:
<script>
$('.photoLink').on('click', function() {
$('html').switchClass('background1', 'background3');
});
</script>
与toggleClass一起尝试过,以检查是否是语义问题,但是toggleClass可以工作,因此无法找到脚本为何不起作用的原因。我在上面引用了一个jQuery库:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
谢谢你的帮助!
最佳答案
class
标记上没有html
属性。将其放在body
标记上。
<body class="background1">
和
...
$('body').switchClass('background1', 'background3');
...
关于jquery - jQuery中的switchClass不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32172543/