0

ユーザーがテキストの横にあるチェックボックスをオンにすると、テキストの色が変わります。

これは私が持っているコードです:

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

これは私がこれまでに持っている機能です:

function hasTickedBox( checkBoxID) {
   var chkbx = document.getElementById( checkBoxID );
   if ( chkbx.checked ) {
      //change the font color 
   } else {
      //do nothing 
   }
}
4

2 に答える 2

0

jqueryを使用していると仮定すると、次のようなことができます

if(chkbx.checked) {
    $(chkbx).parent().css('color', 'new-color');
else {
   $(chkbx).parent().css('color', 'old-color');
} 

jqueryを使用していない場合は、次のようになります。

chxbx.parentNode.style.color = "new-color";

義務的なフィドル:http://jsfiddle.net/Tv5ta/

于 2013-02-19T23:52:00.173 に答える
0

お探しの物件は.style.color

更新されたコード

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


function hasTickedBox( checkBoxID) {
    var chkbx = document.getElementById( checkBoxID );
    if ( chkbx.checked ) {
        document.getElementById('termsP').style.color = "#000000";
    }
}
于 2013-02-19T23:32:17.493 に答える