更改javascript中的字体颜色



这是我的javascript代码:

if (password != confirmPassword)
$("#divCheckPasswordMatch").html("Passwords do not match!");
else
$("#divCheckPasswordMatch").html("Passwords match.");
}

我只想把信息"密码不匹配"的颜色改成红色。我是初学者,有人能帮我吗?

尝试使用这个:

password = '123x';
confirmPassword = '123';
if (password != confirmPassword) {
$("#divCheckPasswordMatch").html("Passwords do not match!").css('color', '#900');
} else {
$("#divCheckPasswordMatch").html("Passwords match.").css('color', '#090');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="divCheckPasswordMatch"></div>

$("#divCheckPasswordMatch").html("Passwords do not match!");

$("#divCheckPasswordMatch").html("<span style='color:red'>Passwords do not match!</span>");
if (password != confirmPassword){
$("#divCheckPasswordMatch").html("<span style='color:red'>Passwords do not match!</span>");
}   else {
$("#divCheckPasswordMatch").html("Passwords match.");
}

$("#divCheckPasswordMatch").css('color', 'red');

在你的1.if^^中

您可以使用css((方法:

if (password != confirmPassword)
{
$("#divCheckPasswordMatch").html("Passwords do not match!");
$("#divCheckPasswordMatch").css('color', 'red');
} else {
$("#divCheckPasswordMatch").html("Passwords match.");
$("#divCheckPasswordMatch").css('color', 'green');
}

您可以在jquery.css文档页面上找到更多详细信息

如果您不想使用jquery,可以采用不同的方法。

thePasswordTextEle = document.createElement("span")
if (password != confirmPassword)
{
thePasswordTextEle.classList.add("red-text")
thePasswordTextEle.innerHTML = "Password ***"
} else {
thePasswordTextEle.classList.add("green-text")
thePasswordTextEle.innerHTML = "Password ***"
}
document.querySelector('#divCheckPasswordMatch').innerHTML = thePasswordTextEle.outerHTML

相关内容

  • 没有找到相关文章

最新更新