如何使用javascript更改占位符的颜色?

Rah*_*ngh 6 html javascript css

我想在调用mobileValidate()后更改此占位符的颜色.

<input type="text" class="form-control" name="InputMobile" id="Mobile"    placeholder="Enter Mobile Number" onblur="mobileValidate()"required>
Run Code Online (Sandbox Code Playgroud)

JavaScript函数是

function mobileValidate(){

    var x = document.getElementById("Mobile");

        if ((x.value).match(re)){
            alert("mobile Number is valid");
        }
        else{

            alert("mobile no is not valid");
            x.value="";
            x.placeholder.style.color="red";
        }

}
Run Code Online (Sandbox Code Playgroud)

dan*_*haj 11

你无法用JavaScript真正修改伪选择器.您必须修改现有元素.

如果可能的话,上课:

.your-class::-webkit-input-placeholder {
    color: #b2cde0
 }
Run Code Online (Sandbox Code Playgroud)

并将其添加到元素:

$('input').addClass('your-class');
Run Code Online (Sandbox Code Playgroud)

或者如果您想使用纯JS,请执行以下操作:

x.classList.add('your-class');
Run Code Online (Sandbox Code Playgroud)