gpt4 book ai didi

javascript - jQuery 替换精确数字

转载 作者:行者123 更新时间:2023-12-02 17:46:17 25 4
gpt4 key购买 nike

我有一些数字从 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/

最佳答案

我建议的方法:

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;
});

JS Fiddle demo .

这种方法避免了 2626通过整个现有文本 t 进行单独评估元素作为 charMap 的键对象并返回相关字符,或者,如果该文本不存在该字符,则返回旧文本本身。

引用文献:

关于javascript - jQuery 替换精确数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21721674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com