2012-10-30 3 views
1

Comment changer Weekend's couleur en bleu?Comment changer la couleur d'un mot en td (jquery)

J'utilise razor et j'ai un drapeau ENUM qui peut directement convertir en chaîne comme ceci:

var a = 1|2|6|7; 
var weekdayStr=a.ToString();//weekdayStr=="Monday, Tuesday, Weekend" 

Et le code html:

<table> 
<tr> 
<td>Monday, Tuesday, Weekend</td></tr></table> 

Comment puis-je changer un seul mot de couleur, pas tout le texte en td?

+3

[Qu'avez-vous essayé?] (Http://whathaveyoutried.com) Astuce: vous » Vous devez envelopper le texte que vous voulez styliser dans une sorte d'élément HTML. –

+0

il pourrait être dans une période, comme ' lundi, Week-end' – yitwail

Répondre

1
$.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'); 

reference from

+0

parfait, merci –

1
$(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) 

}); 
4

Vous devrez soit ajouter manuellement une balise html, soit trouver le td et le modifier avec jQuery.

$('td').html($('td').html().replace('Weekend', '<span>Weekend</span>')); 
$('td span').css('color', 'blue'); 

Something like this fiddle

MISE À JOUR:

new jsFiddle

+0

quand il y a ' lundi, mardi, Week-end' il remplacer à 'lundi, Weekend' –

+0

Quelque chose doit être différent de votre mise en œuvre: [jsFiddle] (http://jsfiddle.net/scrimothy/gFppd/1/) – Scrimothy

+0

essayer '

lundi, Week-end
lundi
Mon jour, mardi, week-end
' –

Questions connexes