在字段集中分隔一个表单,我想突出显示用户正在操作的字段集,这可能是一个解决方案:
<script>
$().ready(function() {
$('form :input').focus(function() {
$(this).closest('fieldset').addClass('f1');
});
$('form :input').blur(function() {
$(this).closest('fieldset').removeClass('f1');
});
});
</script>其中f1是css类
.f1 {
background-color: grey;
transition: 2s;
} 但是我想知道是否存在一个不使用css类的解决方案,我尝试过在没有想要的结果的情况下使用动画()函数。
发布于 2013-11-01 08:39:45
您还必须加载jQuery用户界面,以使颜色具有动画效果--应该注意的是,.animation()函数在jQuery UI中做而不是支持CSS颜色名称。您必须使用十六进制值、rgb[a]()、hsl()等。
$(function () {
$('form :input').focus(function () {
$(this).closest('fieldset').stop(true,true).animate({
"background-color": "#808080" // CSS hex color equivalent of grey
},2000);
});
$('form :input').blur(function () {
$(this).closest('fieldset').stop(true,true)..animate({
"background-color": "transparent"
},2000);
});
});注意:在.animate()方法之前使用 .stop(true,true),以确保如果用户迅速将输入集中并模糊,动画队列不会在堆栈中结束。
http://jsfiddle.net/teddyrised/8wRhG/
但是,我不建议仅仅为了彩色动画而加载jQuery UI。我可以建议使用CSS转换加上使用JS设置字段集背景吗?
$(function () {
$('form :input').focus(function () {
$(this).closest('fieldset').css({
"background-color": "#808080"
});
});
$('form :input').blur(function () {
$(this).closest('fieldset').css({
"background-color": "transparent"
});
});
});CSS:
form fieldset input {
transition: background-color 2s linear;
}https://stackoverflow.com/questions/19723172
复制相似问题