是否有任何资源可以测试jQuery脚本/扩展/插件/版本兼容性问题?
答案 0 :(得分:13)
刚刚写了一个小的jQuery插件来帮助解决版本兼容性问题......随时可以改进它。
(function($) {
/**
* Used for version test cases.
*
* @param {string} left A string containing the version that will become
* the left hand operand.
* @param {string} oper The comparison operator to test against. By
* default, the "==" operator will be used.
* @param {string} right A string containing the version that will
* become the right hand operand. By default, the current jQuery
* version will be used.
*
* @return {boolean} Returns the evaluation of the expression, either
* true or false.
*/
$.isVersion = function(left, oper, right) {
if (left) {
var pre = /pre/i,
replace = /[^\d]+/g,
oper = oper || "==",
right = right || $().jquery,
l = left.replace(replace, ''),
r = right.replace(replace, ''),
l_len = l.length, r_len = r.length,
l_pre = pre.test(left), r_pre = pre.test(right);
l = (r_len > l_len ? parseInt(l) * ((r_len - l_len) * 10) : parseInt(l));
r = (l_len > r_len ? parseInt(r) * ((l_len - r_len) * 10) : parseInt(r));
switch(oper) {
case "==": {
return (true === (l == r && (l_pre == r_pre)));
}
case ">=": {
return (true === (l >= r && (!l_pre || l_pre == r_pre)));
}
case "<=": {
return (true === (l <= r && (!r_pre || r_pre == l_pre)));
}
case ">": {
return (true === (l > r || (l == r && r_pre)));
}
case "<": {
return (true === (l < r || (l == r && l_pre)));
}
}
}
return false;
}
})(jQuery);
可以像这样使用:
$.isVersion("1.4.2"); // returns true, if $().jquery == "1.4.2"
$.isVersion("1.3.2", ">"); // returns true if $().jquery > "1.3.2"
$.isVersion("1.3", ">", "1.2.6"); // returns true
$.isVersion("1.3.2", "<", "1.3.1"); // returns false
$.isVersion("1.4.0", ">=", "1.3.2"); // returns true
$.isVersion("1.4.1", "<=", "1.4.1"); // returns true
还支持预发布(版本比预发行版更重,因此1.4.0pre&lt; 1.4.0):
$.isVersion("1.4.2", "<=", "1.4.2pre"); // returns false
答案 1 :(得分:5)
代码有一些错误,特别是
解
1.由附加的代码确定
2.如果没有完整的重写,这并不是很棘手,而且无论如何都是一个罕见的案例。
(function($) {
/**
* Used for version test cases.
*
* @param {string} left A string containing the version that will become
* the left hand operand.
* @param {string} oper The comparison operator to test against. By
* default, the "==" operator will be used.
* @param {string} right A string containing the version that will
* become the right hand operand. By default, the current jQuery
* version will be used.
*
* @return {boolean} Returns the evaluation of the expression, either
* true or false.
*/
$.isVersion = function(left, oper, right) {
if (left) {
var pre = /pre/i,
replace = /[^\d]+/g,
oper = oper || "==",
right = right || $().jquery,
l = left.replace(replace, ''),
r = right.replace(replace, ''),
l_len = l.length, r_len = r.length,
l_pre = pre.test(left), r_pre = pre.test(right);
l = (r_len > l_len ? parseInt(l) * Math.pow(10, (r_len - l_len)) : parseInt(l));
r = (l_len > r_len ? parseInt(r) * Math.pow(10, (l_len - r_len)) : parseInt(r));
switch(oper) {
case "==": {
return (true === (l == r && (l_pre == r_pre)));
}
case ">=": {
return (true === (l >= r && (!l_pre || l_pre == r_pre)));
}
case "<=": {
return (true === (l <= r && (!r_pre || r_pre == l_pre)));
}
case ">": {
return (true === (l > r || (l == r && r_pre)));
}
case "<": {
return (true === (l < r || (l == r && l_pre)));
}
}
}
return false;
}
})(jQuery);
答案 2 :(得分:3)
没有自动化工具,至少我见过。这样做的原因是jQuery核心团队试图不引入重大变化,除非有真正的长期利益。这意味着当存在重大变化时,您想要的并不是自动化系统总能告诉您的。
让我们以jQuery 1.4为例,这里是一个重大变化列表:
http://jquery14.com/day-01/jquery-14#backwards
jQuery()
现在是一个空集,好还是坏?jQuery.browser.version
现在是浏览器版本,好还是坏?JSON
现在需要更严格的解析,无论好坏?这些只是少数几个,但它们是否会破坏或帮助您的代码通常取决于。现在,如果您有.attr(val, func())
,那么显然这只是1.4+,您可以检测到...因此可以确定您的代码可以使用的最小jQuery版本的引擎。
检查兼容性问题,我认为这意味着在大多数情况下会发生变化,这将更加困难,因为它们(大多数情况下)本质上是非常奇怪的或破坏的情况......否则团队不会打破了他们:))
答案 3 :(得分:1)
您是否想要获取当前版本的jQuery并对其进行测试以查看它是否为某个版本?
$().jquery;
那将获得版本。
大都市
答案 4 :(得分:0)
这是我快速而又肮脏的解决方案:
var versionGTE = function (valueA, valueB) {
var values = [valueA, valueB];
values.sort();
// if valueA > valueB, values will have switched
return (values[1] === valueA);
};
使用示例:
if (versionGTE(jQuery.fn.jquery, "1.3")) {
// don't use @ in attr selectors
}
它在数组上按字母顺序排序。它失败的唯一时间是由于某种原因版本"x.y"
vs "x.y.0"
。在这种情况下,.0
版本被认为更大。它也不支持"pre"
版本。
这是一个较小的版本:
var versionGTE = function (valueA, valueB) {
return ([valueA, valueB].sort()[1] === valueA);
};
如果你担心&#34; rc1&#34;,&#34; pre&#34;或x.0
版本,这是一个更可靠的功能:
var versionCompare = function (versionStringA, versionStringB) {
// quick test of equality before digging in
if (versionStringA === versionStringB) return 0;
var versionedAlpha = /[a-z]+(\d+)/gi,
getArray = function (verString) {
// replace rc1, rc2, beta3, etc with .-1.1, .-1.2, .-1.3, etc
return verString.replace(versionedAlpha, ".-1.$1").split(".");
},
valuesA = getArray(versionStringA),
valuesB = getArray(versionStringB),
maxLength = Math.max(valuesA.length, valuesB.length),
hasLetters = /[a-z]/gi,
// assume any version with letters is -1 (pre, rc, etc)
// also assume that any null entries are 0 (1.5 === 1.5.0)
parseVersion = function (verString) {
return (verString) ? (hasLetters.test(verString)) ? -1 : parseInt(verString, 10) : 0;
};
// verify both arrays are the same size
valuesA.length = maxLength;
valuesB.length = maxLength;
for (var i = 0; i < maxLength; i++) {
var valueA = parseVersion(valuesA[i]),
valueB = parseVersion(valuesB[i]);
if (valueA < valueB) {
return -1;
} else if (valueA > valueB) {
return 1;
}
}
// all equal at this point
return 0;
};
这类似于排序或.compare函数,如果相等则返回0
,如果&gt;则返回1
。 b,和-1
如果a&lt;湾例如:
if (versionCompare(jQuery.fn.jquery, "1.3") >= 0) {
// don't use @ in attr selectors
}
答案 5 :(得分:-3)
压缩版:
(parseInt(jQuery.fn.jquery.split('.').join('')) > 140) ? alert("Running jquery greater than 1.4.0") : alert("current jquery version is 1.4.0 or less");
清晰版的长版:
// get version as a string and get rid of the periods.
version = jQuery.fn.jquery.split('.').join('');
// Make into one long number for easy comparison. Example, 171, or 141.
version = parseInt(version);
if(version > 141){
alert("We're using a version greater than 1.4.1");
}else{
alert("jQuery version is 1.4.1 or lower");
}