![](/img/trans.png)
[英]Why is null in JavaScript bigger than -1, less than 1, but not equal (==) to 0? What is it exactly then?
[英]Why is a bigger variable less than a smaller variable
摘要:
我有一個從數據庫中提取單詞的代碼,將其全部放入一個具有3 x 3文本框的框(您也可以擴展而不是使用3框,這我確認是可行的)。
在框內,它獲取單詞的高度並將該高度設置為保存單詞的div的高度,然后將該高度放置在數組內。 如果div不存在(無文本),則放置0。
每執行3次,即1行,它將檢查該行中三個的高度,並將其上方的div設置為最大高度,如下所示:
var tempN = 0; //Holds each number temporarily
var temp = []; //Holds all 9 box heights
for (var i = 0; i < Panels.length; i++) { //There are 9 panels
temp[i] = new Array(); //adding to array
if (document.getElementById('Column'+i+Id[Ten])!=null) {//Id[Ten] will be 3, 2, and 1, in a for loop
document.getElementById('Column'+i+Id[Ten]).style.height = document.getElementById('Text'+i+Id[Ten]).clientHeight+"px";
temp[i].push(document.getElementById('Column'+i+Id[Ten]).clientHeight); //puts number on table
} else {
temp[i].push(0); //since there is no panel, the height would be 0
}
if (i%3 == 2) { //every 3rd time
for (var x=2;x >= 0;x--) { //also could do (var x=0;x<3;x++)
alert(temp[i-x]+" is greater than "+tempN); //tells me what should work
if (tempN < temp[i-x]) { //also could do the other way around
tempN = temp[i-x]; //tempN will be the current biggest height
alert(tempN+' '+temp[i-x]); //just a show it went through if
document.getElementById('Row'+BoxRow+Id[Ten]).style.height = tempN + "px";
}
}
tempN = 0; //reset tempN for next 3 boxes
}
}
temp = []; //reset temp for next box
現在,我的頁面的方式是,temp將是:
temp = [0,158,0,0,158,0,0,50,0] when Id[Ten] is 3
temp = [0,50,0,0,0,0,0,0,0] when Id[Ten] is 2
temp = [284,50,50,50,50,50] when Id[Ten] is 1
問題:
當我的代碼到達Id [Ten]為1時,它將開始於:
“ 284大於0”
這是計划好的,然后我得到:
“ 284 284”
也已計划,然后:
“ 50大於284”
這是問題所在:此后應該跳過的內容是:
“ 50 50”
這告訴我,如果if語句看到284 <50,則通過了True。我一生中至少做過一次數學運算,並且我知道284比50大。因此,我的盒子有一個高度比其中的文字小,並且它們的重疊程度比我小。 我確定我的代碼和我正處於某種中年危機,如果有人可以向我解釋我的代碼為何要經歷這個叛逆階段,那將是非常不錯的。
這是因為您正在比較字符串與字符串,所以在比較字符串js時僅驗證字母順序。
您必須在變量中使用parseInt。
這是我在chrome控制台中進行的測試。
var test1 = 50
var test2 = 200;
test1 > test2
false
--------
var test1 = "50"
var test2 = "200";
test1 > test2
true
parseInt(test1) > parseInt(test2)
false
您的代碼正在比較字符串(字母順序):
"Zoo" > "Apple" // true
"50" > "284" // true
您需要使用諸如parseInt之類的數字進行比較:
parseInt("50") > parseInt("284"); // false
var stringResult = "50" > "284"; // true var intResult = parseInt("50") > parseInt("284"); // false document.getElementById('strings').textContent = stringResult; document.getElementById('ints').textContent = intResult;
<h3>Result of "50" > "284" = <span id="strings"></span> </h3> <h3> Result of parseInt("50") > parseInt("284") = <span id="ints"></span> </h3>
在javascript中,一切都是對象。它沒有任何類型,它通過我們聲明的方式確定類型。在這里,您正在比較(tempN <temp [ix])tempN和temp [ix]都是字符串,您必須將它們轉換為數字然后比較
var number1=parseInt(tempN,10);
var number2=parseInt(temp[i-x],10);
console.log(number1<number2);
如果要轉換數字數組以進行排序但不按字母順序排列,可以執行以下操作
var numbers=[1,90,5,8];
numbers.sort(SortCompare);
function SortCompare(a,b){
return a-b;
}
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.