我很新于jquery,正在尝试在按钮点击后更改页面背景颜色。
我已经搜索了很多类似的问题,但是我正在寻找一个关于页面背景颜色更改的答案。
我会感激任何帮助,谢谢,Freddie
HTML:
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<section class="text">
CLICK THE BUTTON TO<br> CHANGE THE BACKGROUND!
</section>
<button id="btn">CLICK ME!</button>
</body>
</html>
CSS:
body {
height: ;
background: #D8D8D8;
}
.text {
color: #686868;
font-family: arial;
font-size: 2em;
text-align: center;
margin-top: 50px;
margin-bottom: 20px;
}
#btn {
margin-left: 550px;
width: 200px;
height: 100px;
font-size: 2em;
border-radius: 10px;
background: #ffffff;
}
#btn:hover {
background: #ff0000;
color: #ffffff;
}
JS:
$(button).click(function() {
$(this).css('background', '#ff0000');
)};
$(button)
改为$('#btn')
,并将整个代码块包装在$(document).ready(function() { });
中,这样就可以了! - zgood