這是在括號與文本的HTML:如何使用jquery替換HTML中括號內的文本?
<div class="someClass" style="width: 100%; ">Dealing flop: [Ks, 5c, 8h]</div>
這是我想結束了:
<div class="someClass" style="width: 100%; ">Dealing flop: [<span style='color: #000;>K♠</span>, <span style='color: #000;>5♣</span>, <span style='color: #f00;>8♥</span>]</div>
我想這:
$('.someClass').each(function(){
$(this).addClass("km_done");
var tt = $(this).html();
if(tt.indexOf("[")!=-1){
var cards = tt.slice(tt.indexOf("[")+1 ,tt.indexOf("]")).split(", ");
$.each(cards,function(id,val){
$(this).replaceWith(tt.replace(val,getColor(val)))
});
}
});
getColor = function(str){
var col;
switch(str.charAt(1)){
case "h": col = "<span style='color: red; font-weight:bold;'>"+str.charAt(0)+"♥</span>";
break;
case "d": col = "<span style='color: red; font-weight:bold;'>"+str.charAt(0)+"♦</span>";
break;
case "s": col = "<span style='color: black; font-weight:bold;'>"+str.charAt(0)+"♠</span>";
break;
case "c": col = "<span style='color: black; font-weight:bold;'>"+str.charAt(0)+"♣</span>";
break;
default: col = "exception getColor > "+str;
}
return col;
}
但你可以猜到,它不起作用:(
我在做什麼錯?
謝謝大家的答案!因爲我只能選擇一個,所以我選擇了那個有漂亮正則表達式的那個。 :) –