jQuery: Change specific text color

戏子无情 提交于 2021-02-04 21:13:07

问题


I can't seem to figure this out. I have the code below in my web page, how do I change the "Click here" to blue or some color using jQuery?

<td width="30%" valign="top" align="left" 
class="td-label-276-7365 td-label-276 labeltext" 
id="td-label-field-7365">Flavors [Click here] :
</td>

Thank you in advance.


回答1:


If you only want to change the color of "Click here" and you absolutely must use JS, then you can do:

var text = $('td').text();
text = text.replace(/\[Click\shere\]/g, '[<font color="blue">Click here</font>]');
$('td').html(text);

But it is more advisable to just do it directly with html and css, like:

<td width="30%" ...>Flavors [<span class="blue">Click here</span>] :</td>

and in your CSS:

span.blue {
   color: blue;
}

Demo: Both approaches can be seen here




回答2:


use css()

 $('#td-label-field-7365').css('color','blue');

updated

if you need to change color just in [Click here], thn ad span around it and use css()

html

<td width="30%" valign="top" align="left" 
class="td-label-276-7365 td-label-276 labeltext" 
id="td-label-field-7365">Flavors <span>[Click here]</span>
</td>

jquery

$('#td-label-field-7365 span').css('color','blue');



回答3:


$('.labeltext').css('color','blue');



回答4:


$('#td-label-field-7365').css('color', 'blue');


来源:https://stackoverflow.com/questions/15386381/jquery-change-specific-text-color

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!