使用javascript更改文本框边框颜色 [英] changing textbox border colour using javascript
问题描述
我在做表单验证。当表单输入不正确时,我在文本框中添加一个红色边框:
I'm doing form validation. when the form input is incorrect i add a red border to the textbox:
document.getElementById("fName").style.borderColor="#FF0000"
这样就给我一个2px的红色边框。我想做的是如果用户putas在一个正确的值,以回到原始边框...
this then gives me a 2px red border. what i want to do is if the user putas in a correct value to go back to the original border...
有人可以告诉我如何改变边框的大小和color in javascript
can someone tell me how i change the border size and colour in javascript
推荐答案
使用CSS类别替换CSS样式
Use CSS styles with CSS Classes instead
CSS
.error {
border:2px solid red;
}
现在使用Javascript
$ b
Now in Javascript
document.getElementById("fName").className = document.getElementById("fName").className + " error"; // this adds the error class
document.getElementById("fName").className = document.getElementById("fName").className.replace(" error", ""); // this removes the error class
我提到这个的主要原因是假设你想改变颜色的错误元素的边界。如果你选择你的方式,你可能需要修改代码中的很多地方。如果你选择我的方式,你可以简单地编辑样式表。
The main reason I mention this is suppose you want to change the color of the errored element's border. If you choose your way you will may need to modify many places in code. If you choose my way you can simply edit the style sheet.
这篇关于使用javascript更改文本框边框颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!