为什么 javascript if/else 语句不起作用
Why does the javascript if/else statement not working
我试着写了一小段代码,但是没有用。
它应该如何工作:
有一个 if 函数语句,它在 iPhone/iPod 和 else
之间过滤
问题是它不起作用。
$(function() {
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
function openNav() {
document.getElementById("mySidenav").style.width = "360px";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
}
} else {
function openNav() {
document.getElementById("mySidenav").style.width = "360px";
document.getElementById("main").style.marginLeft = "360px";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft= "0";
document.body.style.backgroundColor = "white";
}
}
});
坦克你们!
您需要做的就是在别处声明函数,然后在 if/else 语句中调用它们。
1-声明所有函数:
function openNav() {
document.getElementById("mySidenav").style.width = "360px";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
}
function openNavWithMargin() {
document.getElementById("mySidenav").style.width = "360px";
document.getElementById("main").style.marginLeft = "360px";
}
function closeNavWithMargin() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft= "0";
document.body.style.backgroundColor = "white";
}
2-在if/else语句中调用它们:
$(function() {
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
openNav();
closeNav();
} else {
openNavWithMargin();
closeNavWithMargin();
}
});
您需要根据自己的需要编写如下代码。
function openNav() {
document.getElementById("main").style.marginLeft = "360px"; //common code
if(!((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))) {
document.getElementById("main").style.marginLeft = "360px";
}
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0"; //common code
if(!((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))) {
document.getElementById("main").style.marginLeft= "0";
document.body.style.backgroundColor = "white";
}
}
$(function() {
// Call your function from here as per the requirement.
openNav();
closeNav();
});
我试着写了一小段代码,但是没有用。
它应该如何工作: 有一个 if 函数语句,它在 iPhone/iPod 和 else
之间过滤问题是它不起作用。
$(function() {
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
function openNav() {
document.getElementById("mySidenav").style.width = "360px";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
}
} else {
function openNav() {
document.getElementById("mySidenav").style.width = "360px";
document.getElementById("main").style.marginLeft = "360px";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft= "0";
document.body.style.backgroundColor = "white";
}
}
});
坦克你们!
您需要做的就是在别处声明函数,然后在 if/else 语句中调用它们。
1-声明所有函数:
function openNav() {
document.getElementById("mySidenav").style.width = "360px";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
}
function openNavWithMargin() {
document.getElementById("mySidenav").style.width = "360px";
document.getElementById("main").style.marginLeft = "360px";
}
function closeNavWithMargin() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft= "0";
document.body.style.backgroundColor = "white";
}
2-在if/else语句中调用它们:
$(function() {
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
openNav();
closeNav();
} else {
openNavWithMargin();
closeNavWithMargin();
}
});
您需要根据自己的需要编写如下代码。
function openNav() {
document.getElementById("main").style.marginLeft = "360px"; //common code
if(!((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))) {
document.getElementById("main").style.marginLeft = "360px";
}
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0"; //common code
if(!((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))) {
document.getElementById("main").style.marginLeft= "0";
document.body.style.backgroundColor = "white";
}
}
$(function() {
// Call your function from here as per the requirement.
openNav();
closeNav();
});