我的 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>
最佳答案
这个 fiddle 怎么样?
label
标记的 for
属性引用了 input
的 id
属性,而不是它的 name
。
我也将样式移到了 css。
关于javascript - jQuery 控制标签的边框颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8030560/