下面是一些简单的代码,它采用“.somedivs
”的id值,并将它们放入全局变量中,然后由条件语句排序,然后再输出到控制台。下面你可以看到我得到一些奇怪的结果,无论我使用什么条件组合,我都无法让它正常工作。看起来很基本,我在想我违反了js的某些定律,或者我错过了一些明显的东西。
$(document).ready(function(){
$('.somedivs').mousedown(function(){
first = $(this).attr("id");
}).mouseup(function(){
second = $(this).attr("id");
if(window.first > window.second){
var higher = window.first;
var lower = window.second;
var process = 1;
}else if(window.second > window.first){
var higher = window.second;
var lower = window.first;
var process = 2;
}else if(window.first === window.second){
var higher = window.first;
var lower = window.second;
var process = 3;
}
if(higher > lower){
var status = true;
}else{
var status = false;
}
console.log("process = " + process + " // " + "window.first = "+window.first+" / window.second = "+window.second + " // higher = "+higher+" / lower = "+lower + " // status = "+ status );
});
控制台输出,正如您可以看到的那样,如果您自己查看数字,大多数行都会返回true。我标记了箭头“>”中的一些错误。 为什么状态应为false时才为真?
process = 2 // window.first = 0 / window.second = 1 // higher = 1 / lower = 0 // status = true
process = 3 // window.first = 2 / window.second = 2 // higher = 2 / lower = 2 // status = false
process = 1 // window.first = 2 / window.second = 0 // higher = 2 / lower = 0 // status = true
> process = 1 // window.first = 5 / window.second = 22 // higher = 5 / lower = 22 // status = true
process = 3 // window.first = 7 / window.second = 7 // higher = 7 / lower = 7 // status = false
process = 2 // window.first = 3 / window.second = 9 // higher = 9 / lower = 3 // status = true
process = 1 // window.first = 19 / window.second = 14 // higher = 19 / lower = 14 // status = true
> process = 1 // window.first = 5 / window.second = 11 // higher = 5 / lower = 11 // status = true
process = 1 // window.first = 35 / window.second = 23 // higher = 35 / lower = 23 // status = true
process = 2 // window.first = 13 / window.second = 17 // higher = 17 / lower = 13 // status = true
process = 1 // window.first = 32 / window.second = 24 // higher = 32 / lower = 24 // status = true
process = 2 // window.first = 17 / window.second = 18 // higher = 18 / lower = 17 // status = true
process = 1 // window.first = 22 / window.second = 18 // higher = 22 / lower = 18 // status = true
> process = 1 // window.first = 8 / window.second = 11 // higher = 8 / lower = 11 // status = true
process = 1 // window.first = 18 / window.second = 14 // higher = 18 / lower = 14 // status = true
process = 2 // window.first = 3 / window.second = 6 // higher = 6 / lower = 3 // status = true
process = 1 // window.first = 24 / window.second = 16 // higher = 24 / lower = 16 // status = true
process = 1 // window.first = 9 / window.second = 15 // higher = 9 / lower = 15 // status = true
process = 1 // window.first = 31 / window.second = 26 // higher = 31 / lower = 26 // status = true
process = 2 // window.first = 24 / window.second = 28 // higher = 28 / lower = 24 // status = true
process = 1 // window.first = 41 / window.second = 30 // higher = 41 / lower = 30 // status = true
process = 2 // window.first = 13 / window.second = 19 // higher = 19 / lower = 13 // status = true
答案 0 :(得分:1)
我认为这是因为变量的类型:
尝试:
first = parseInt($(this).attr("id"));
和
second = parseInt($(this).attr("id"));
答案 1 :(得分:0)
据我所见, 更高总是更高(所以状态应该总是真的)不? 你总是将最高值(根据测试)影响到更高。 所以除了平等(过程3),状态始终为真。
问题似乎更像是:为什么有时候更高的数字不是输出中的最高数字?
而这正是由梁良亮提出的,你在比较字符串。 为了比较字符串,javascript逐字符地比较它们,例如在第一个>
中“5”高于“22”,因为“5”高于“2”。
如果您希望您的代码使用数字值,那么您将使用Zhengliang Zheng建议的parseInt方法。
编辑:如果您希望它的状态为true或false,具体取决于哪个更高(并且您不关心更高/更低的值),那么
var status=(parseInt(window.first)>parseInt(window.second));
应该做的伎俩