为什么这个逻辑运算符不起作用?
Why is this logical operator not working?
我正在使用代码
setInterval(function() {
if (document.querySelector("#shrimpCook01").style.top > "400px") {
alert("789")
}
}, 200);
查看 shrimpcook01 的位置,但即使 div 的最高值小于 400px,我也会收到警告消息。
它将对字符串进行字典顺序比较。
使用 parseInt 将值转换为数字,并将其与 400 数字进行比较,而不是 "400px" 字符串。
您可以轻松转换字符串值。
setInterval(function() {
const height = document.querySelector("#shrimpCook01").style.top.split('px')[0];
if (Number(height) > 400) {
alert("789")
}
}, 200);
我正在使用代码
setInterval(function() {
if (document.querySelector("#shrimpCook01").style.top > "400px") {
alert("789")
}
}, 200);
查看 shrimpcook01 的位置,但即使 div 的最高值小于 400px,我也会收到警告消息。
它将对字符串进行字典顺序比较。 使用 parseInt 将值转换为数字,并将其与 400 数字进行比较,而不是 "400px" 字符串。
您可以轻松转换字符串值。
setInterval(function() {
const height = document.querySelector("#shrimpCook01").style.top.split('px')[0];
if (Number(height) > 400) {
alert("789")
}
}, 200);