我有一些数字从0到3200的div,并希望用一个字符替换它们。
html示例:
<div>2</div>
<div>6</div>
<div>26</div>
jQuery的例子:
$('div').text(function () {
return $(this).text().replace("1","K").replace("2","/").replace("3","@").replace("4","r").replace("5","[").replace("6","[").replace("7","[").replace("8",",").replace("9",",").replace("10","J").replace("11",",").replace("12",",").replace("13","P").replace("14","]").replace("15","]").replace("16","]").replace("17","R").replace("18","R").replace("19","N").replace("20","N").replace("21","N").replace("22","N").replace("23","W").replace("24","L").replace("25","d").replace("26","D").replace("27","(").replace("28","B").replace("29","b").replace("30","B").replace("31","a").replace("32","A").replace("33","f").replace("34","F").replace("35","[").replace("36",":").replace("37","@").replace("38","@").replace("39","!").replace("40","?").replace("41","Z").replace("42","]").replace("43","Z").replace("44","B").replace("45","@").replace("46","]").replace("47","@").replace("0","V").replace("3200","c");
});
我得到的2 - / 正确,6 - [正确,26 - / <错误表示26是D。
请帮我解决我的jQuery代码吗?在这里直播jsFiddle:http://jsfiddle.net/UEy7w/
答案 0 :(得分:1)
我建议采用这种方法:
var charMap = {
'1' : 'K',
'2' : '/',
'3' : '@',
'4' : 'r',
'6' : '[',
'26' : 'D'
// please fill in the rest yourself
};
$('div').text(function (i,t) {
return charMap[t] || t;
});
此方法通过将元素的整个现有文本2
作为{{{1}的关键字传递,避免单独评估6
26
t
charMap
1}} object并返回相关的字符,或者,如果该文本不存在,则返回旧文本本身。
参考文献:
答案 1 :(得分:1)
在更换2之前你应该更换26 ..否则26中的2将全部被替换
答案 2 :(得分:0)
首先,替换最长的数字,例如26
。然后,替换2
。这样,2
中的26
将不会被替换,然后才能“识别”26
。
这应该可以解决问题:
$('div').text(function () {
return $(this).text()
.replace("3200","c")
.replace("10","J")
.replace("11",",")
.replace("12",",")
.replace("13","P")
.replace("14","]")
.replace("15","]")
.replace("16","]")
.replace("17","R")
.replace("18","R")
.replace("19","N")
.replace("20","N")
.replace("21","N")
.replace("22","N")
.replace("23","W")
.replace("24","L")
.replace("25","d")
.replace("26","D")
.replace("27","(")
.replace("28","B")
.replace("29","b")
.replace("30","B")
.replace("31","a")
.replace("32","A")
.replace("33","f")
.replace("34","F")
.replace("35","[")
.replace("36",":")
.replace("37","@")
.replace("38","@")
.replace("39","!")
.replace("40","?")
.replace("41","Z")
.replace("42","]")
.replace("43","Z")
.replace("44","B")
.replace("45","@")
.replace("46","]")
.replace("47","@")
.replace("0","V")
.replace("1","K")
.replace("2","/")
.replace("3","@")
.replace("4","r")
.replace("5","[")
.replace("6","[")
.replace("7","[")
.replace("8",",")
.replace("9",",");
});
答案 3 :(得分:0)
26分别被2和6取代。在两位数替换代码后放置单个数字替换代码。这个:
$('div').text(function () {
return $(this).text().replace("10","J").replace("11",",").replace("12",",").replace("13","P").replace("14","]").replace("15","]").replace("16","]").replace("17","R").replace("18","R").replace("19","N").replace("20","N").replace("21","N").replace("22","N").replace("23","W").replace("24","L").replace("25","d").replace("26","D").replace("27","(").replace("28","B").replace("29","b").replace("30","B").replace("31","a").replace("32","A").replace("33","f").replace("34","F").replace("35","[").replace("36",":").replace("37","@").replace("38","@").replace("39","!").replace("40","?").replace("41","Z").replace("42","]").replace("43","Z").replace("44","B").replace("45","@").replace("46","]").replace("47","@").replace("0","V").replace("3200","c").replace("1","K").replace("2","/").replace("3","@").replace("4","r").replace("5","[").replace("6","[").replace("7","[").replace("8",",").replace("9",",");
});
<强> Demo 强>
答案 4 :(得分:0)
替换功能按从左到右的顺序执行。因此,在调用替换26
的代码之前,已经替换了2和6。
解决方案:将对.replace()
的调用重新排序,确保链中的none不会破坏任何未来(=向右)更换。
更好的解决方案:无论你在做什么,都不要这样做 - 它看起来像是失败了。
答案 5 :(得分:0)
这是因为您在已经替换的文本上递归调用.replace()。
这就是您的代码现在的工作方式。
"26".replace(2,'\')
"\6".replace((6,'[')
如果您将.replace('26',D)置于所有其他替换之前,它将按预期工作。
这是小提琴:http://jsfiddle.net/bEqwP/1/
但这不是你应该怎么做的。我说这只是为了让你了解幕后发生的事情。我会建议David Thomas提到的方法