简体   繁体   English

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

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

Make the text saying “I have read and agree to the terms and conditions” change to the colour black and remove the bold if the user checks the terms and conditions checkbox, and return to the original formatting if they uncheck the checkbox如果用户选中了条款和条件复选框,则将“我已阅读并同意条款和条件”的文本更改为黑色并删除粗体,如果取消选中该复选框,则返回原始格式

Code from php file来自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>

what should be the javascript file code should be by using document.getElementById javascript 文件代码应该是什么,使用 document.getElementById

在此处输入图片说明

I do not have your code but I am supposing what you need is something like this.我没有你的代码,但我假设你需要的是这样的。

you gonna have your HTML like this你会有这样的 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>

And your javascript should be something like this.你的 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