如何将Weekend's
颜色变为蓝色?
我正在使用razor
,我有一个标志枚举,可以直接转换为这样的字符串:
var a = 1|2|6|7;
var weekdayStr=a.ToString();//weekdayStr=="Monday, Tuesday, Weekend"
和html:
<table>
<tr>
<td>Monday, Tuesday, Weekend</td></tr></table>
如何只更改一个单词的颜色,而不是 td 中的所有文本?
您必须手动添加一个 html 标记,或者找到 td 并使用 jQuery 对其进行修改。
$('td').html($('td').html().replace('Weekend', '<span>Weekend</span>'));
$('td span').css('color', 'blue');
更新:
$(document).ready(function(){
var build = '';
var data = $('#yourtd_id').html().split(',');
$.each(data,function(v){
if(v == 'Weekend'){
build+='<span style="color:blue">'+v+'</span>,';
}else{
build+='<span>'+v+'</span>,';
}
});
$('#yourtd_id').html(build)
});
$.fn.replaceText = function( search, replace, text_only ) {
return this.each(function(){
var node = this.firstChild,
val,
new_val,
remove = [];
if ( node ) {
do {
if ( node.nodeType === 3 ) {
val = node.nodeValue;
new_val = val.replace( search, replace );
if ( new_val !== val ) {
if ( !text_only && /</.test( new_val ) ) {
$(node).before( new_val );
remove.push( node );
} else {
node.nodeValue = new_val;
}
}
}
} while ( node = node.nextSibling );
}
remove.length && $(remove).remove();
});
};
$("td").replaceText( /Weekend/g, "<span>Weekend</span>" );
$('td span').css('color', 'blue');