简体   繁体   English

为什么这个JS自调用函数不起作用?

[英]Why doesn't this JS self-invoked function work?

I'm trying to use an auto self-invoked functions to open and close the menu but it doesn't work. 我正在尝试使用自动调用功能来打开和关闭菜单,但是它不起作用。

I rely on examples I saw about self-invoked functions but now I don't know what's wrong. 我依靠我看到的有关自调用函数的示例,但是现在我不知道出了什么问题。

I tried different syntax to call the function, but nothing worked but everything worked when I Call the funtion using "onclick=function name", so I guess that the problem is on the self-invoked function. 我尝试了不同的语法来调用该函数,但是当我使用“ onclick =函数名”调用该功能时,什么都没有起作用,但是一切都起作用了,所以我想问题出在自调用函数上。

<body>
  <nav class="navbar">
    <span class="open-slide" >
      <a href="#">
        <img src="img/menu.png" id="btn-open">
      </a>
    </span>
    <h1 id="t1">Innova<span id="t2">TEK</span> </h1>    
  </nav>

  <div id="side-menu" class="side-nav">
    <a href="#" class="btn-close">
    <img src="img/back.png" alt="" id="btn-close"></a>
    <a href="#"><img i class = "menu"src="img/home.png" alt=""> &nbsp;Home</a>
    <a href="#"> <img src="img/about.png" class = "menu"  alt=""> &nbsp; About</a>
    <a href="#"><img src="img/support.png" alt="">&nbsp; Services</a>
    <a href="#"> <img src="img/contact.png" alt="">&nbsp; Contact</a>
  </div>
  <div id="main">
    <h1>Responsive Side Menu</h1>
  </div>

(function(){
    show();
    hide();
});

function show(){
    document.getElementById("btn-open").addEventListener('click', menuShow());
}

function hide(){
document.getElementById("btn-close").addEventListener("click", menuHide());
}

function menuShow(){
    document.getElementById('side-menu').style.width = '17%';
    document.getElementById('main').style.marginLeft = '17%';
}  

function menuHide(){
    document.getElementById('side-menu').style.width = '0';
    document.getElementById('main').style.marginLeft = '0';
}

You are missing the () at the end of the IIFE without which the function will not call itself. 您会在IIFE末尾缺少() ,否则该函数将不会自行调用。 Also you should have passed the function reference to the event listeners and not a function call. 同样,您应该将函数引用传递给事件侦听器,而不是函数调用。

Here is working code, I added some css for the btn-close so we can see it for testing. 这是工作代码,我为btn-close添加了一些css ,以便我们进行测试。

 (function(){ show(); hide(); })(); // <--- were missing the () here function show(){ document.getElementById("btn-open").addEventListener('click', menuShow); } function hide(){ document.getElementById("btn-close").addEventListener("click", menuHide); } function menuShow(){ document.getElementById('side-menu').style.width = '17%'; document.getElementById('main').style.marginLeft = '17%'; } function menuHide(){ document.getElementById('side-menu').style.width = '0'; document.getElementById('main').style.marginLeft = '0'; } 
 #btn-close { position:relative; float:left; height:20px; width: 20px; border: 1px solid #f00; } 
 <nav class="navbar"> <span class="open-slide" > <a href="#"> <img src="img/menu.png" id="btn-open"> </a> </span> <h1 id="t1">Innova<span id="t2">TEK</span> </h1> </nav> <div id="side-menu" class="side-nav"> <a href="#" class="btn-close"> <img src="img/back.png" alt="" id="btn-close"></a> <br style="clear:both" /> <a href="#"><img i class = "menu"src="img/home.png" alt=""> &nbsp;Home</a> <a href="#"> <img src="img/about.png" class = "menu" alt=""> &nbsp; About</a> <a href="#"><img src="img/support.png" alt="">&nbsp; Services</a> <a href="#"> <img src="img/contact.png" alt="">&nbsp; Contact</a> </div> <div id="main"> <h1>Responsive Side Menu</h1> </div> 

friend, the functions with addEventListener should not have the parentheses to be invoked, just delete the parentheses 朋友,具有addEventListener的函数不应具有要调用的括号,而只需删除括号

answer it is: 答案是:

document.getElementById("btn-open").addEventListener('click', menuShow );
document.getElementById("btn-close").addEventListener('click', menuHide );
function menuShow(){
    document.getElementById('side-menu').style.width = '17%';
    document.getElementById('main').style.marginLeft = '17%';
} 



function menuHide(){
    document.getElementById('side-menu').style.width = '0';
    document.getElementById('main').style.marginLeft = '0';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么函数在对象文字中被自动调用? - Why is a function being self-invoked within an object literal? 递归自调用函数和异步操作 - Recursive self-invoked function and asynchronous operations 如何在自调用的jquery函数中获取php值? - how get a php value inside a self-invoked jquery function? 如何将这个命名的自调用函数传递给参数? - How is this named self-invoked function being passed an argument? 在自调用 function 末尾有括号或在 javascript 中没有括号有什么区别? - what is the difference between has parentheses at the end of self-invoked function or has not in the javascript? NodeJS:在对象中创建条件属性的方法比自调用函数更短? (ES5或ES6) - NodeJS: shorter way to create a conditional property in an object than self-invoked function? (either ES5 or ES6) 当前代码需要是 IIFE(自调用) - 代码工作并运行 - Current code needs to be IIFE(Self-Invoked) - Code works and runs 比较预先声明和自调用的匿名函数 - Comparing Pre-declared and Self-invoked Anonymous Functions 为什么这个js数组函数不起作用? - Why doesn't this js array function work? 在函数内部并被调用时,JS代码的行为不同 - JS code doesn't act the same when inside a function, and invoked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM