简体   繁体   English

更改文字输入边框颜色

[英]Change text input border color

I want to make a form where data is verified using JavaScript before being sent. 我想做一个表格,其中在发送之前使用JavaScript验证数据。 When a field is empty, I want to set its border to red. 当字段为空时,我要将其边框设置为红色。

HTML code: HTML代码:

<label>Question: </label><input type = "text" maxlength = "100" name = "question"> <br />

JavaScript code 1: JavaScript代码1:

fields[i].style.borderColor = "red";

JavaScript code 2: JavaScript代码2:

fields[i].style.border = "1px solid red";

If I use JS code 1, the border changes its color but it has the width bigger than before (even though I do not say anything about border width). 如果我使用JS代码1,则边框会更改其颜色,但宽度会比以前大(即使我没有说边框宽度)。 If I use JS code 2, the text input shrinks with 2px and the change is noticeable. 如果我使用JS代码2,则文本输入将缩小2px,并且更改非常明显。

What should I do to change only the border color? 我该怎么做才能仅更改边框颜色?

Actually this is preferred by adding and removing classes: 实际上,这是通过添加和删除类来实现的:

$("input").change(function()
  {
     var value = $(this).val();
     if(value=="")
     {
          $(this).addClass("red-border");
          $(this).focus();
     }else
     {
          $(this).removeClass("red-border");
     }
  });

And your CSS: 还有你的CSS:

.red-border{
    border: 1px solid red;
}

The default user agent stylesheet uses this for the input field: 默认的用户代理样式表将其用于​​输入字段:

border: 2px inset;

Now you may ask why is this not defined by default? 现在您可能会问为什么默认情况下未定义?

by default(In IE the appreance is hard-coded): 默认情况下(在IE中,外观是硬编码的):

appearance: textfield;

But whenever you change something: 但是,每当您更改某些内容时:

appearance: none;

And when the appearance is none, you will see the 2px inset border. 并且当appearance为空时,您将看到2px插入边框。

So actually the width is the problem here: 所以实际上宽度是这里的问题:

So you want to change 2 propeties: Border-width and border-color 因此,您要更改2种属性: Border-widthborder-color
You would need 2 lines now: 您现在需要两行:

document.getElementsByTagName('input')[0].style.border = "red";
document.getElementsByTagName('input')[0].style.borderWidth = "1px";

jsFiddle jsFiddle

However your own solution might be elegant, as it is defined with one line of code: 但是,您自己的解决方案可能很优雅,因为它是用一行代码定义的:

fields[i].style.border = "1px solid red";


Note that the inset style sets the top and right border lighter where the bottom and left border is the given color. 请注意,插入样式会设置顶部和右侧边框变亮,其中底部和左侧边框是给定的颜色。 Setting the style to solid will solve this. 将样式设置为solid将解决此问题。

It won't harm your code to use the whole shorthand property of border. 使用border的整个简写属性不会损害您的代码。 You always have to be very specific when you want to win the battle with the user agent stylesheet . 当您想赢得用户代理样式表的胜利时,必须始终非常明确

I have something like this in production, only it uses alerts instead of color change. 我在生产中有这样的事情,只是它使用警报而不是颜色更改。 Use CSS Styles & classes: 使用CSS样式和类:

CSS 的CSS

.error {
    border:2px solid red;
}

JavaScript 的JavaScript

<script>
function checkField(){
    var f = document.getElementById('<name of field>').value;
    if (f === "") {
       document.getElementById('<name of field>').className = document.getElementById('<name of field>').className + " error";
       return false;
    }
}
</script>

Then add this to your button/control's click event: 然后将其添加到按钮/控件的click事件中:

return checkField()

This SO post seems to be similar: changing textbox border colour using javascript 这样的帖子似乎很相似: 使用javascript更改文本框边框颜色

Use outline instead of border. 使用轮廓线代替边​​框。

fields[i].style.outline = "1px solid red" ; fields[i].style.outline = "1px solid red" ;

Try this out. 试试看 Jquery jQuery的

 $("input").change(function ()
  {
     var value = this.value;
     if(value=="")
     {
          $(this).css("border", "1px solid red");
     }else
     {
        $(this).css("border",'');
     }
  }).trigger("change");

Html HTML

  <input type="text" class="col"> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM