Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果使用类名填充,请更改输入样式_Javascript_Jquery_Html_Class_If Statement - Fatal编程技术网

Javascript 如果使用类名填充,请更改输入样式

Javascript 如果使用类名填充,请更改输入样式,javascript,jquery,html,class,if-statement,Javascript,Jquery,Html,Class,If Statement,我发现fiddle可以在填充时更改输入样式。但它使用的是ID。我想使用类名。我使用了document.getElementsByClassName(),但它不起作用。有人能帮忙吗?谢谢 这是密码 html Javascript function checkFilled() { var inputVal = document.getElementById("subEmail"); if (inputVal.value == "") { inputVal.style.backgroundC

我发现fiddle可以在填充时更改输入样式。但它使用的是ID。我想使用类名。我使用了
document.getElementsByClassName()
,但它不起作用。有人能帮忙吗?谢谢

这是密码

html

Javascript

function checkFilled() {
var inputVal = document.getElementById("subEmail");
if (inputVal.value == "") {
    inputVal.style.backgroundColor = "yellow";
}
else{
    inputVal.style.backgroundColor = "";
}
}

checkFilled();
这里是指向返回dom元素数组的链接,因此您需要使用
inputVal[0]
安装的
inputVal

函数checkFilled(){
var inputVal=document.getElementsByClassName(“子电子邮件”);
如果(inputVal[0]。值=“”){
inputVal[0].style.backgroundColor=“黄色”;
}否则{
inputVal[0]。style.backgroundColor=“”;
}
}
检查填充()
返回dom元素数组,因此需要使用
inputVal[0]
instaed of
inputVal

函数checkFilled(){
var inputVal=document.getElementsByClassName(“子电子邮件”);
如果(inputVal[0]。值=“”){
inputVal[0].style.backgroundColor=“黄色”;
}否则{
inputVal[0]。style.backgroundColor=“”;
}
}
检查填充()
上述函数返回与数组具有相同类名的所有元素。您可能需要获得数组中的第一个元素,如

document.getElementsByClassName("subEmail")[0]

上述函数返回与数组具有相同类名的所有元素。您可能需要获得数组中的第一个元素,如

document.getElementsByClassName("subEmail")[0]

我建议传递调用事件的元素的引用

HTML

功能检查填充(elem){
elem.style.backgroundColor=elem.value==“”?“黄色”:“”;
}
window.onload=函数(){
已填写复选框(document.getElementsByClassName(“子电子邮件”)[0]);
}

我建议传递调用事件的元素的引用

HTML

功能检查填充(elem){
elem.style.backgroundColor=elem.value==“”?“黄色”:“”;
}
window.onload=函数(){
已填写复选框(document.getElementsByClassName(“子电子邮件”)[0]);
}

这是小提琴的工作原理

函数checkFilled(){
var inputVals=document.getElementsByClassName(“子电子邮件”);

对于(var i=0;i,这是小提琴的工作原理

函数checkFilled(){
var inputVals=document.getElementsByClassName(“子电子邮件”);
对于(var i=0;i
<input type="text" onchange="checkFilled(this);"/>
function checkFilled(elem) {
    elem.style.backgroundColor = elem.value == "" ? "yellow" : "";
}
function checkFilled() {
    var inputVals = document.getElementsByClassName("subEmail");

for(var i=0; i<inputVals.length;i++){
    if (inputVals[i].value == "") {
        inputVals[i].style.backgroundColor = "yellow";
    }
    else{
        inputVals[i].style.backgroundColor = "";
    }
    }
}

checkFilled();
<input type="text" id="subEmail" class="subEmail" onchange="checkFilled();"/>