【问题标题】:Why does the javascript if/else statement not working为什么 javascript if/else 语句不起作用
【发布时间】:2021-03-04 13:12:00
【问题描述】:

我试着写了一个小代码,但是没有用。

它应该如何工作: 有一个 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 进行逻辑处理,然后调用该函数,而不是在括号内声明新函数。
  • 不要有条件地定义函数;相反,将条件移动到函数中。
  • 什么不起作用?您的代码目前什么都不做。
  • 另外,这一切都可以通过 CSS 完成,通过添加和删除类以及使用媒体查询。

标签: javascript user-agent


【解决方案1】:

您需要根据您的要求编写如下代码。

 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();
});

【讨论】:

    【解决方案2】:

    您需要做的就是在别处声明函数,然后在 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();
      }
        
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多