如何使用javascript更改占位符的颜色? [英] How to change the colour of placeholder using javascript?
本文介绍了如何使用javascript更改占位符的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在调用mobileValidate()后更改此占位符的颜色。
<input type="text" class="form-control" name="InputMobile" id="Mobile" placeholder="Enter Mobile Number" onblur="mobileValidate()"required>
JavaScript函数
JavaScript function is
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";
}
}
推荐答案
您无法使用JavaScript真正修改伪选择器。您必须修改现有元素。
You can't really modify pseudo-selectors with JavaScript. You'll have to modify an existing a element.
如果可能,请上课:
.your-class::-webkit-input-placeholder {
color: #b2cde0
}
并将其添加到元素:
$('input').addClass('your-class');
或者如果你想使用纯JS,请执行以下操作:
Or if you want to use pure JS, do this:
x.classList.add('your-class');
这篇关于如何使用javascript更改占位符的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文