[英]Why doesn't the jQuery background animation work?
I'm a beginner web designer, and though I would try my hand at JavaScript and jQuery. 我是一名初学者Web设计师,尽管我会尝试使用JavaScript和jQuery。 After looking around W3Schools for a bit, I tried to make a simple jQuery animation, but it doesn't seem to work.
在浏览了一下W3Schools之后,我尝试制作一个简单的jQuery动画,但是它似乎不起作用。
<html>
<head>
<script type="text/javascript" src="jQuery.js"></script>
<script>
$(document).ready(function(){
$("#name").blur(funtion(){
if(value==null || value==""){
$("#name").animate({background:#D23E42}, "slow");
}
});
});
</script>
<style type="text/css">
body
{
font-family: Arial;
}
#name
{
background:#6BAA64;
color: #FFFFFF;
border:2px none;
padding:5px;
-webkit-border-radius:8px 8px;
-moz-border-radius:8px 8px;
border-radius:8px 8px;
text-align: center;
}
</style>
</head>
<body style="text-align: center;">
Name:
<br />
<input id="name" type="text" value="Your Name" />
</body>
</html>
I was just trying to setup a simple contact/registration form model, so when the name field was left empty, or wasn't changed, would turn red (on blur). 我只是尝试设置一个简单的联系/注册表单模型,所以当姓名字段为空或未更改时,它将变成红色(模糊)。
You got many mistakes in that code: 您在该代码中有很多错误:
$("#name").blur(function() {
if (!this.value) {
$(this).css('background-color', '#D23E42');
}
});
Mistakes: 错误:
funtion
=> function
funtion
=> function
value
=> this.value
. value
=> this.value
。 null
, it can be only an empty string. null
,只能是一个空字符串。 animate
=> css
, and the reason is that you can't animate background-color changes without a plugin. animate
=> css
,原因是没有插件就无法为背景颜色更改设置动画。 #D23E42
=> '#D23E42'
#D23E42
=> '#D23E42'
background
=> background-color
. background
=> background-color
。 $("#name")
=> $(this)
$("#name")
=> $(this)
It might be a good idea to stop learning at w3school as it doesn't seem to pay off... 停止在w3school学习可能是个好主意,因为这似乎没有回报...
Sorry but you can't animate background, unless you use jColor 抱歉,除非您使用jColor ,否则您不能设置背景动画
All animated properties should be animated to a single numeric value, except as noted below;
除非另有说明,否则所有动画属性都应设置为单个数值。 most properties that are non-numeric cannot be animated using basic jQuery functionality (For example, width, height, or left can be animated but background-color cannot be , unless the jQuery.Color() plugin is used).
大多数非数字属性不能使用基本的jQuery功能进行动画处理(例如,可以对width,height或left进行动画处理, 而background-color不能进行动画处理,除非使用jQuery.Color()插件)。
jQuery.animate
无法设置背景颜色,而只能设置width, height, left, top
等属性。要设置背景颜色,必须使用https://github.com/jquery/jquery-color这样的插件
You have two mistakes: 您有两个错误:
http://www.bitstorm.org/jquery/color-animation/ http://www.bitstorm.org/jquery/color-animation/
$("#name").focusout(function(){
var value = $.trim($(this).val());
if(value === ''){
$(this).animate({backgroundColor:'#D23E42'}, 500);
}
});
===
to compare values ===
比较值 EDIT Hede is a demo without the UI EDIT Hede是一个没有UI的演示
You surely need to redo the color if the user reenter some text: 如果用户重新输入一些文本,您肯定需要重做颜色:
$("#name").focusout(function(){
var value = $.trim($(this).val());
if(value === ''){
$(this).css({background:'#D23E42'});
}else{
$(this).css({background:'#6BAA64'});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.