简体   繁体   中英

Change children font color on click?

I'm trying to change font color on click, but unfortunetely it doesnt change. What is the correct way to do it ?

$("#hepsi").click(function(){

$(this).removeClass('boskolon2').addClass('boskolon').siblings('.boskolon').removeClass('boskolon').addClass('boskolon2');
$(this).children("img").not("img.profimg").hide();
$(this).children("font").css({color: 'red'});

});

html

<div id="hepsi" class="boskolon2">
<img class="profimg" src="misal.png" width=36 \><div>
<b><font id="probas">AkifFF</font></b><br>22, Mersin</div>
</div>

在此处输入图片说明

使用.find()代替.children()请注意.children()仅行进的单级下调DOM树。

$(this).find("font").css({"color": 'red'});

use toggleClass, when you want to change a color, or some css rule dont do it with js, is slower than css

$("#hepsi").click(function(){
   $(this).toggleClass('myColor');
});

css code

.bosklon {
   color: ...;
 }

.bosklon.myColor{
  color : ...;
 }

html

<div id="hepsi" class="bosklon">
<img class="profimg" src="misal.png" width=36 \><div>
<b><font id="probas">AkifFF</font></b><br>22, Mersin</div>
</div>

I think that the font tag is not supported in html 5, so I'd use CSS instead font tag, you can use this: $(this).children("b").css({color: 'red'});

and remove font tag.

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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