前端 - 用jQuery改变背景颜色没反应
本文介绍了前端 - 用jQuery改变背景颜色没反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
<style type="text/css">
div{
margin: 0 auto;
width: 300px;
height: 300px;
background-color: #333;
}
</style>
</head>
<body>
<div>
</div>
<script type="text/javascript" src="js/jquery-3.0.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("div").click(function(){
$(this).animate({
backgroundColor: "#eee"
},1000);
})
})
</script>
解决方案
$(document).ready(function(){
$("div").click(function(){
this.animate({
backgroundColor: "#eee"
},1000);
})
})
这里的this指向的是对应div的原生DOMElement对象,而不是JQuery对象,改为以下代码即可:
$(this).animate({
backgroundColor: "#eee"
},1000);
另外,animate没有实现对颜色的渐变,可以通过在css中设置动画渐变transition,然后js通过修改类名或者样式来达到颜色渐变的效果。
这篇关于前端 - 用jQuery改变背景颜色没反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文