javascript - 每次单击按钮时如何更改我的背景

标签 javascript jquery html css jquery-mobile

大家好,我正在为 children 制作一个数学游戏,我希望用户在点击按钮时能够更改他们游戏的背景图片 我有一个太空/丛林/沙漠主题我尝试了我在网上找到的这段代码,但它不起作用,但它在这里起作用 http://jsfiddle.net/Eqdfs/27/ 怎么会呢?

请记住,我在原始程序中使用的是 jquery mobile,并且我使用此代码设置了默认背景

 <style>
body {
    background-image: url("http://s21.postimg.org/9bj52fal3/sdfsdfdsf.jpg");
    background-repeat: no-repeat;
    background-position: right top;
    margin-right: 200px;
    background-attachment: fixed;
    background-size:100% 100%;
}
.ui-page {
    background: transparent;
}
.ui-content{
    background: transparent;
}

</style>

然后我发现这段代码声称它可以添加按钮来更改背景图像,但我无法让它在这个简单的 html 文件中工作,但在我的实际游戏中却独自一人

<!DOCTYPE html>
<html>
<head>

<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset = "utf-8">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<link rel="stylesheet" type="text/css" href="mystyle.css">
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script src="//http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.13/jquery-ui.js></script>
<script src="//http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.13/jquery-ui.min.js></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui-touch-punch/0.2.3/jquery.ui.touch-punch.min.js"></script>

<style>
body {
    background-repeat:repeat;
    background-position:top-left;
}

body.class1 {
    background-image:url('http://www.dca.fee.unicamp.br/~lotufo/Courses/ia-636-1995/alberto/proj5/html/pattern_Id.gif');
}

body.class2 {
    background-image:url('http://upload.wikimedia.org/wikipedia/commons/e/ec/Pattern_square_16.png');
}

body.class3 {
    background-image:url('http://www.herbactive.com.br/catalog/view/theme/default/image/pattern/pattern-11.png');
}
</style>

<script>
$("#btn1").click(function() {
 $('body').removeClass();
 $('body').addClass('class1');
});

$("#btn2").click(function() {
 $('body').removeClass();
 $('body').addClass('class2');
});

$("#btn3").click(function() {
 $('body').removeClass();
 $('body').addClass('class3');
});
</script>

</head>
<body>

<a id="btn1">Noise BG</a><br/>
    <a id="btn2">RVB BG</a><br/>
    <a id="btn3">dunno BG</a>   

</body>
</html>

最佳答案

真正的问题是 jquery-mobilejquery-UI 相互冲突。您的设计中也几乎没有标记问题。试试这段代码并检查它是否有效

编辑 更新了 jquery。现在,只需单击一下即可更改背景。

<!DOCTYPE html>
<html>

<head>

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta charset="utf-8">
  <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
  <link rel="stylesheet" type="text/css" href="mystyle.css">
  <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui-touch-punch/0.2.3/jquery.ui.touch-punch.min.js"></script>

  <style type="text/css">
    body {
      background-repeat: repeat;
      background-position: top-left;
    }
    a#btn1,
    a#btn2,
    a#btn3 {
      color: #fff;
      cursor: pointer;
      background: #333;
      padding: 0px 15px;
    }
    body.class1 {
      background-image: url('http://www.dca.fee.unicamp.br/~lotufo/Courses/ia-636-1995/alberto/proj5/html/pattern_Id.gif');
    }
    body.class2 {
      background-image: url('http://upload.wikimedia.org/wikipedia/commons/e/ec/Pattern_square_16.png');
    }
    body.class3 {
      background-image: url('http://www.herbactive.com.br/catalog/view/theme/default/image/pattern/pattern-11.png');
    }
  </style>

</head>

<body>

  <a id="btn1">Noise BG</a>
  <br/>
  <a id="btn2">RVB BG</a>
  <br/>
  <a id="btn3">dunno BG</a> 

  <script>
    $("#btn1").click(function() {
      $('body').removeAttr('class');
      $('body').attr('class', 'class1');
    });

    $("#btn2").click(function() {
      $('body').removeAttr('class');
      $('body').attr('class', 'class2');
    });

    $("#btn3").click(function() {
      $('body').removeAttr('class');
      $('body').attr('class', 'class3');
    });
  </script>

</body>

</html>

关于javascript - 每次单击按钮时如何更改我的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34235405/

相关文章:

javascript - 我想在每次点击时隐藏下一个 div。但是找不到解决办法

javascript - 根据条件在悬停时添加类名

javascript - 通过.html() 更改div 中的内容时,jquery 滚动条消失了,如何解决?

javascript - 如何只触发一次增量

javascript - 在 GoogleApp 电子表格中,如何使 onEdit 事件处理程序对拖动复制使用react?

php - 使用 Jquery Ajax 后 mootools AJAX 出现问题

jquery - 使用 jQuery 进行字符串日期和字符串操作

html - 为标题中的 block 着色

javascript - 如何编写 Angularjs 嵌套嵌入

javascript - 评估JavaScript在WkWebview上缓慢(非常慢)地绘制数据