2010-08-30 26 views
1

我想单独的DIV持有的输入也改变背景颜色。我如何将它添加到我的现有代码中?添加元素以改变现有的onblur

<script type="text/javascript"> 
jQuery(document).ready(function() { 
jQuery("input:[type=text]").focus(function() { 
jQuery(this).addClass("highLightInput"); 

}); 
jQuery("input:[type=text]").blur(function() { 
jQuery(this).removeClass("highLightInput"); 
}); 
    }); 
</script> 

回答

2
<script type="text/javascript"> 
$(document).ready(function() { 
$("input:[type=text]").focus(function() { 
$(this).addClass("highLightInput"); 
$(this).parent('div').addClass('highlight'); 

}); 
$("input:[type=text]").blur(function() { 
$(this).removeClass("highLightInput"); 
$(this).parent('div').removeClass('highlight'); 
}); 
    }); 
</script> 
+0

我试了,只有输入的变化。这里是我的html:

Erik 2010-08-30 19:26:55

+0

@Erik Moin的代码看起来正确。你有为''div id =“quote_input”>'定义的'highlight'类吗? – Pat 2010-08-30 19:30:46

+0

感谢Moin ...你的帮助做到了。我发现了一个错字。非常感谢。 – Erik 2010-08-30 19:30:46

0

停止使用JavaScript乱搞的东西,可以用CSS来解决。唯一的限制是,它不支持IE6/7

textarea.yourClass { 
    width: 100%; 
    background: white url('iamges/img.png') no-repeat 50% 50%; 
    color: grey; 
} 
textarea.yourClass:focus { 
    color: #444; 
    background: none; 
} 

如果你确实需要支持IE6/7,然后用上面的方法JS