简体   繁体   English

选中复选框时将文本更改为普通文本

[英]Change text to normal when checkbox is checked

I am new to CSS and JavaScript and I've tried to change the text from a checkbox to normal when it's pressed, but I haven't succeded... In the beginning, the text contains some bolded words, but after it'll be clicked, all the phrase should be set to font-weight: normal... 我是CSS和JavaScript的新手,我曾尝试将其从复选框更改为正常时的文本,但是我没有成功...最初,文本包含一些粗体字,但之后单击,所有短语都应设置为font-weight:正常...

<FORM>

        <input type="checkbox" name="adresa1 onClick="GetBold(this);" id="mail1"> adresa@expeditor.com - <B>Subiectbold daca email necitit</B> - data primire <BR>  
        <input type="checkbox" name="adresa" > joey@gmail.com - <B>Subiectbold daca email necitit</B> - data primire </a><BR>
        <input type="checkbox" name="adresa" > adresa@expeditor.com - <B>Subiectbold daca email necitit</B> - data primire <BR>

</FORM>

and I've used the following function, but it's not working: 并且我使用了以下功能,但无法正常工作:

<script>
function GetBold(current)
{
  var array = document.getElementById("mail1");

  for (var i = 0; i < array.length; i++)
  {
    array[i].style.fontWeight = 'normal';
   }

  current.style.fontWeight = 'bold';
 }
</script>

You could easily use CSS for this, with the adjacent-sibling combinator ( + ), using the :checked pseudo-class: 您可以通过:checked伪类与相邻的兄弟组合器( + )轻松地使用CSS:

input[type=checkbox]:checked + b {
  font-weight: normal;
}

 input[type=checkbox]:checked + b { font-weight: normal; } 
 <FORM> <input type="checkbox" name="adresa1" id="mail1">adresa@expeditor.com - <b>Subiectbold daca email necitit</b> - data primire <br /> <input type="checkbox" name="adresa">joey@gmail.com - <b>Subiectbold daca email necitit</b> - data primire <br /> <input type="checkbox" name="adresa">adresa@expeditor.com - <b>Subiectbold daca email necitit</b> - data primire <br /> </FORM> 

Also, remove the trailing white-space from your attribute-values; 另外,从属性值中删除尾随空格; it serves no purpose and, in the case of an id , #elementID does not match <a id="elementID "> , which actively serves to complicate things. 也是没有用处的,在一个的情况下, id#elementID不匹配<a id="elementID "> ,其积极作用是复杂的事情。

References: 参考文献:

Since you are a begginer as you said you first should try to do something like this: 由于您是您所说的初学者,因此您首先应该尝试执行以下操作:

<form>
        <input type="checkbox" name="adresa1" onClick="GetBold(this);" id="mail1"> adresa@expeditor.com - <label id="lbl1">Subiectbold daca email necitit</label> -data primire <BR> 
</form>


<script>
function GetBold(current)
{
    var checkB= document.getElementById(current.id);
    var labelText = document.getElementById("lbl1");

if(checkB.checked)
    labelText.style.fontWeight = 'bold';
else
        labelText.style.fontWeight = 'normal';
 }
</script>

Then when you understand how that works create two classes in CSS because it's not good to decorate your text in html (using tag) better option will be to create two classes like this: 然后,当您了解其工作原理后,在CSS中创建两个类,因为用html装饰文本(使用标签)不是很好,更好的选择是创建两个这样的类:

<style>
.normal{
font-weight:normal;
}
.bold{
font-weight:bold;
}
</style>

Then in javascript get all your elements that you want to change (using getElementsByTagName): 然后在javascript中获取要更改的所有元素(使用getElementsByTagName):

function GetBold(current)
{
  var checkB = document.getElementById(current.id);
 var array = document.getElementsByTagName("label"); //get all label tags from html

  for (var i = 0; i < array.length; i++)
  {
    array[i].className = "normal"; //changing class from css for all yours labels
   }
 }

All your text that you want to change should be in tag label: 您要更改的所有文本都应在标签标签中:

<input type="checkbox" name="adresa1" onClick="GetBold(this);" id="mail1"> adresa@expeditor.com - <label class="bold">Subiectbold daca email necitit</label> - data primire <BR>  
    <input type="checkbox" > joey@gmail.com - <label class="bold">Subiectbold daca email necitit</label> - data primire <BR> 
    <input type="checkbox" name="adresa"> adresa@expeditor.com - <label class="bold">Subiectbold daca email necitit</label> - data primire <BR> 

Notice that there is class name "bold" instead of tag, that same class name will be changed in javascript function mentioned above 请注意,存在类名“ bold”而不是标签,该类名将在上述javascript函数中更改

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

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