我想在js中使用具有相同名称的函数,基本上有一个链接:

<a onClick="showjury1();" >show</a>


当用户在桌面上时,我希望执行以下代码:

if (window.screen.width >= 1150) {
  function showjury1() {
    document.getElementById("jury1").style.display = "block";
    document.getElementById("juryline1").style.display = "none";
  }
}


当用户使用移动设备或其他分辨率时,我希望执行以下代码:

if (window.screen.width <= 500) {
  function showjury1() {
    document.getElementById("jury1").style.display = "none";
    document.getElementById("juryline1").style.display = "block";
  }
}


这可能吗?我已经执行了它,它给出了错误;未定义jury1等。

最佳答案

为什么需要两次定义该函数?只要正确使用条件语句

function showjury1()
{
    if(window.screen.width >= 1150)
    {
         document.getElementById("jury1").style.display = "block";
         document.getElementById("juryline1").style.display = "none";
    }
    else if(window.screen.width <= 500)
    {
        document.getElementById("jury1").style.display = "none";
        document.getElementById("juryline1").style.display = "block";
    }
    else
    {
       // might want to do something here too
    }
}

关于javascript - 具有相同名称的Javascript函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36396183/

10-12 12:39
查看更多