jquery - jquery 中的 switchClass 不起作用

标签 jquery html css

我有一个网站,其中 html 标签使用 css 有背景图片。我已经创建了一个标签,当你点击它时,html 标签背景图像应该通过 CSS 改变。我正在使用 switchClass 但它不起作用。在下面找到我的代码:

  1. 行:

<html class="background1">

  1. 带有触发 jQuery 的链接:

<div id=photoNum>
    <a class="photoLink" href="#">1</a>
  </div>

  1. 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;
}

  1. 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>

感谢您的帮助!

最佳答案

html 标签上没有class 属性。而是将它放在 body 标签上。

<body class="background1">

...
$('body').switchClass('background1', 'background3');
...

关于jquery - jquery 中的 switchClass 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32172543/

相关文章:

jquery - 改变div的背景颜色一段时间,然后返回到上一个

android - 触摸事件点击背后的内容phonegap zepto

javascript - 如果选中,更改复选框的标签 css 类

html - Flexbox适合 child 的宽度

javascript - 如何使用变量作为图像文件夹路径

javascript - 我怎样才能使用 jQuery 触发多个 ID?

html - 导航栏和页面顶部之间的间隙

javascript - 如何: Onclick buttons and text area scripts

javascript - 拒绝应用来自 'http://localhost:3000/css/style.css' 的样式,因为它的 MIME 类型 ('text/html' ) 不是受支持的样式表 MIME 类型

javascript - 隐藏后如何删除li空间?