我的表单有多个输入文本框,其中有一些浅色文本消息。当用户输入某些内容时,文本应该变黑。 我有一个可以运行的 jquery 代码,但我不能将其用于多个文本框
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.js"></script>
<style>
input{color:grey}
</style>
</head>
<body>
<div class="formWrapper">
<form name=form>
<Input type="text" name="name1" value="Eg : Flat-27,Block 4,Skyline" size="30">
</form>
</div>
<script>
$('input').focus(function(){
if($(this).val() == this.defaultValue){$(this).val('');$(this).css("color","Red");}
}).blur(function(){
if($(this).val() == ''){$(this).val(this.defaultValue);$(this).css("color","grey");}
});
</script>
</body>
</html>
最佳答案
按照这些思路应该可以工作:
// jQuery
$("input[type='text']").keyup(function(){
if ($(this).val() !== ''){
$(this).css('color', '#ff0000')
} else {
$(this).css('color', '#000000')
}
});
关于javascript - 在文本框中输入内容时更改文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8008256/