繁体   English   中英

如果使用javascript选中复选框,如何更改文本颜色和字体粗细?

[英]How to change text colour and font weight if checkbox is checked with javascript?

如果用户选中了条款和条件复选框,则将“我已阅读并同意条款和条件”的文本更改为黑色并删除粗体,如果取消选中该复选框,则返回原始格式

来自php文件的代码

<p style="color: #FF0000; font-weight: bold;" id='termsText'>I have read and agree to the terms and conditions <input type="checkbox" name="termsChkbx"></p>

javascript 文件代码应该是什么,使用 document.getElementById

在此处输入图片说明

我没有你的代码,但我假设你需要的是这样的。

你会有这样的 HTML

    <p style="color: #FF0000; font-weight: bold;" id='termsText'>
I have read and agree to the terms and conditions
<input type="checkbox" name="termsChkbx"></p>

你的 javascript 应该是这样的。

var input1=document.getElementsByName('termsChkbx')[0];
input1.onclick=function(){
    if(input1.checked){
    document.getElementById('termsText').style="color:black; font-weight: bold;";
    }else{
    document.getElementById('termsText').style="color:red; font-weight: bold;";
  }

}

暂无
暂无

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

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