简体   繁体   English

更改模糊元素的边框颜色

[英]change element border-color on blur

I'm trying to change my text box's border color on blur 我正在尝试在模糊时更改文本框的边框颜色

var checking = document.getElementById("checking");
function checkTextField() {
       document.getElementById("checking").style.border = "2px solid green";
}

function blurEventFunc() {
      if (document.getElementById("checking") === ''){
        document.getElementById("checking").style.border = "2px solid red";
    } else {
       document.getElementById("checking").style.border = "2px solid green";
    }
}

您将需要检查值而不是整个元素

if (document.getElementById("checking").value === '')

you need to check if the value is empty, not the element its self. 您需要检查值是否为空,而不是元素本身。 also inside the blur function, you can refer to the element using the this keyword 同样在模糊功能中,您可以使用this关键字引用元素

 function blurEventFunc() { if (this.value === ''){ this.style.border = "2px solid red"; } else { this.style.border = "2px solid green"; } } document.getElementById("checking").addEventListener('blur', blurEventFunc); 
 <input type="text" id="checking" /> 

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

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