我的jQuery函数如下所示
$('input[type="text"]').focus(function() {
$("label").css("border-bottom-color", "red");
});
$('input[type="text"]').blur(function() {
$("label").css("border-bottom-color", "#e6e6e6");
}); 1)我的表单中有一堆文本输入。我想要做的是改变聚焦文本框标签的下边框颜色(每个文本框都有一个标签。我只想改变聚焦的文本框标签的边框颜色)。但是我的函数会一次改变所有标签的边框颜色。如何解决这个问题?
2)我有两个表单。id是form1和form2。我想对第二种形式做同样的事情,但颜色将是另一种。如何修改这个函数?
我的表单看起来就像这样
<form id="form1">
...
<label for="fname">First Name</label>
<input name="fname" placeholder="please enter your first name" type="text" />
...
</form>
<form id="form2">
...
<label for="job">Your Job</label>
...
<input name="job" placeholder="please enter your job" type="text" />
</form>发布于 2011-11-07 05:46:46
发布于 2011-11-07 06:04:05
同时使用CSS和JavaScript:
$('input:text, input:password, textarea').focus(
function(){
$(this).prev('label').addClass('focused');
}).blur(
function(){
$(this).prev('label').removeClass('focused');
});并且,在CSS中:
#form1 label.focused {
border-bottom: 1px solid red;
}
#form2 label.focused {
border-bottom: 1px solid green;
}发布于 2011-11-07 05:39:17
对于问题1,使用$(this)作为选择器:
$('input[type="text"]').focus(function() {
$(this).css("border-bottom-color", "red");
});
$('input[type="text"]').blur(function() {
$(this).css("border-bottom-color", "#e6e6e6");
});对于问题2,您的意思是,在用户按任一顺序选择了这两个项目之后?它们不可能同时聚焦在一起。
https://stackoverflow.com/questions/8030560
复制相似问题