问题描述
我正在尝试将一些JavaScript代码从MicrosoftAjax移动到JQuery。我使用流行的.net方法的MicrosoftAjax中的JavaScript等价物,例如String.format(),String.startsWith()等。在jQuery中它们是否等价?
I'm trying to move some JavaScript code from MicrosoftAjax to JQuery. I use the JavaScript equivalents in MicrosoftAjax of the popular .net methods, e.g. String.format(), String.startsWith(), etc. Are there equivalents to them in jQuery?
推荐答案
供您参考,因此您可以选择它并将要继续使用的部分包含在单独的JS文件中。或者,你可以将它们移植到jQuery。
The source code for ASP.NET AJAX is available for your reference, so you can pick through it and include the parts you want to continue using into a separate JS file. Or, you can port them to jQuery.
这是格式函数...
String.format = function() {
var s = arguments[0];
for (var i = 0; i < arguments.length - 1; i++) {
var reg = new RegExp("\\{" + i + "\\}", "gm");
s = s.replace(reg, arguments[i + 1]);
}
return s;
}
这里是endsWith和startsWith原型函数......
And here are the endsWith and startsWith prototype functions...
String.prototype.endsWith = function (suffix) {
return (this.substr(this.length - suffix.length) === suffix);
}
String.prototype.startsWith = function(prefix) {
return (this.substr(0, prefix.length) === prefix);
}
这篇关于相当于jQuery中的String.format的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!