本文介绍了更改< div>的背景颜色部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在按下按钮时使用jQuery
更改<div>
部分的背景颜色,下面是执行此操作的代码.为什么不起作用?
I'm trying to use jQuery
to change the background color of a <div>
section when pressing on a button, and below is the code to do that. Why isn't it working?
HTML文件
<html>
<head>
<script type="text/javascript" src="jquery-1.6.4.js"></script>
<script type="text/javascript" src="script.js"></script>
<style type="text/css">
#name{
background-color: #FFFFF2;
width: 100px;
}
</style>
</head>
<body>
<input type="button" id="bgcolor" value="Change color"/>
<div id="name">
Abder-Rahman
</body>
</html>
script.js
$("bgcolor").click(function(){
$("#name").animate(
{backgroundColor: '#8B008B'},
"fast");}
);
谢谢.
推荐答案
您有几个问题:
- 您的选择器错误:
$("#bgcolor")
- 您丢失了
</div>
结束标记. - 您需要 jQueryUI 来对
background-color
进行动画处理.
- Your selector is wrong:
$("#bgcolor")
- You're missing a closing
</div>
tag. - You need jQueryUI to animate
background-color
.
HTML:
<input type="button" id="bgcolor" value="Change color"/>
<div id="name">
Abder-Rahman
</div>
JS:
$("#bgcolor").click(function(){
$("#name").animate(
{backgroundColor: '#8B008B'},
"fast");}
);
您的代码已修复(并与jQueryUI一起使用): http://jsfiddle.net/andrewwhitaker/rAVj9 /
Your code, fixed (and working with jQueryUI included): http://jsfiddle.net/andrewwhitaker/rAVj9/
这篇关于更改< div>的背景颜色部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!