我的代码使用调整大小函数 bool 值随宽度变化, 但我的函数包含 if 语句不读取 bool 值, 我的代码有什么问题?
我使用了 = ==,但我不知道如何准确使用它们
var footerMenu = ['#about', '#help']
var slideSwitch = false
function slideDown(){
footerMenu.forEach(function(list){
$(list+' p').click(function(){
$(list+' ul').slideToggle(300)
})
})
}
function slideClear(){
for( let i = 0; i < footerMenu.length; i++){
$(footerMenu[i]+' p').click(function(){
var choice = footerMenu[i]
footerMenu.splice(i, 1);
footerMenu.forEach(function(list){
$(list+' ul').slideUp(300)
})
footerMenu.splice(i, 0, choice);
})
}
}
function slideTotal(){
if(slideSwitch = true){
slideDown()
slideClear()
}
}
$(document).ready(function() {
checkWidth();
});
$(window).resize(function(){
checkWidth();
});
function checkWidth(){
$vWidth = $(window).width();
if($vWidth <576){
console.log("Width: Less than 576");
slideSwitch = true
}else{
console.log("Width: More than 576");
slideSwitch = false
}
}
slideTotal()
我希望纠正我的代码,如果我没有任何错误,请告诉我如何解决
最佳答案
=
是赋值运算符。它改变了左侧的值。它通常不是您在 if 语句中想要的内容,除非您真的知道自己在做什么。
==
和===
是比较运算符。它们之间的不同之处在于==
自动将两个参数转换为相同类型,而 ===
才不是。但这并不重要,我的建议是始终使用 ===
比较 if 语句中的变量。
此外,与 true ( x === true
) 进行比较是完全没有意义的,因为 if 语句已经这样做了;这是不必要的。
(旁注:您可以执行 if (x === true === true === true)
,这与 if (x)
相同。)
您想要的代码是:
function slideTotal() {
if (slideSwitch) {
slideDown();
slideClear();
}
}
关于javascript 如果状态函数不读取 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56416962/