{0} 的 Jquery 字符串格式问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20729823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery string format issue with {0}
提问by milandjukic88
This not works and I can't figure it out why...
这不起作用,我无法弄清楚为什么......
alert("milan {0}".format("djukic"));
My jquery
version is 1.6.2.
I tried everything and no success..
我的jquery
版本是 1.6.2。我尝试了一切,但没有成功..
回答by boxmein
This isn't an error related to jQuery - it's the fact that you're trying to run a string instance's method that doesn't exist. A quick search gives me another StackOverflow answer, which happily implements String#format
for you.
这不是与 jQuery 相关的错误 - 这是您尝试运行不存在的字符串实例方法的事实。快速搜索为我提供了另一个 StackOverflow 答案,它很高兴String#format
为您实现。
String.prototype.format = function() {
var str = this;
for (var i = 0; i < arguments.length; i++) {
var reg = new RegExp("\{" + i + "\}", "gm");
str = str.replace(reg, arguments[i]);
}
return str;
}
Just paste that at the top or near to the top of your script file and use your newly-created function like "the {0} jumps over the {1}".format("quick brown fox", "lazy dog");
in the script.
只需将其粘贴到脚本文件的顶部或顶部附近,然后像"the {0} jumps over the {1}".format("quick brown fox", "lazy dog");
在脚本中一样使用新创建的函数。