【问题标题】:jQuery - Background color not changing [duplicate]jQuery - 背景颜色不变[重复]
【发布时间】:2011-11-29 07:48:27
【问题描述】:

可能重复:
jQuery animate backgroundColor
Changing the background color of a <div> section

我有以下代码试图在按下按钮后更改文本的背景颜色。情况是它没有做它打算做的事情。这是为什么?这里出了什么问题。

HTML 代码

<html>
<head>
<script type="text/javascript" src="jquery-1.6.4.js"></script>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.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>
</div>
</html>

script.js

$("#bgcolor").click(function(){
    $("#name").animate(
        {backgroundColor: '#8B008B'},
        "fast");}
);

编辑

我想注意到我有一个名为:jquery-ui-1.8.16.custom 的文件夹,这是我将这些文件放入的位置。而且,我引用了 jquery-1.6.4,js,如图所示除了引用当前文件夹中 js 文件夹中的 jquery-ui-1.8.16.custom.min.js 之外,我还将它放在同一个文件夹中。

我在这里做错了什么?这不就是引用jQuery和jQueryUI的方式吗?

使用的浏览器: Mozilla Firefox 6.0.2

文件夹结构:

jquery-ui-1.8.16.custom/abc.html

jquery-ui-1.8.16.custom/script.js

jquery-ui-1.8.16.custom/jquery-1.6.4.js

jquery-ui-1.8.16.custom/js/jquery-ui-1.8.16.custom.min.js

谢谢。

【问题讨论】:

  • 为什么要包含 jQuery 两次?
  • It works here。也许是因为你的结束标签错了?即&lt;/body&gt;&lt;/div&gt; 而不是&lt;/div&gt;&lt;/body&gt;?
  • 我刚刚把它放到了一个 jsfiddle 中,它似乎奏效了。 jsfiddle.net/PGHHt 也许尝试摆脱第二个 jquery 包含?或者其中一些文件可能不存在?

标签: jquery background-color


【解决方案1】:

尽管(抱歉)您的代码看起来很糟糕(html 标记的顺序错误,您包含 jquery 两次,...) animate() 不适用于开箱即用的 backgroundColor,您将需要颜色插件。 mu 太短了,在他对您帖子的评论中发布了指向相关主题的链接。

【讨论】:

  • 这就是 jQueryUI 的用途!
  • 你说得对,我只用jquery检查过。谢谢。
【解决方案2】:
<script type="text/javascript" src="jquery-1.6.4.js"></script>
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script>

参考点的路径是否正确?删除引用后,它正在处理jsfiddle

【讨论】:

    猜你喜欢
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 2014-06-16
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多