简体   繁体   English

我正在尝试添加关闭功能

[英]I am trying to add the close function

I have created an email subscription form at site footer. 我已经在网站页脚创建了电子邮件订阅表单。 But the problem is, when i click the close icon, the form do not disappears. 但是问题是,当我单击关闭图标时,表单不会消失。

 var button = document.querySelector("#close"); button.addEventListener("click", function() { var t = document.querySelector(div.getAttribute("data-target")); t.style.display = "none" == t.style.display ? "block" : "none" }); 
 <div class="hook-section js-common-hook"> <div class="hook-content small-hook-show"> <div class="loader"> Loading... </div> <div class="hook-close-btn sprite-img">Close</div> <div class="top-sec clearfix"> <div class="left-sec sprite-img"> </div> <div class="right-sec"> <h5>Email Newsletter</h5> <p>The best of technology updates from India and around the world</p> </div> </div> <div class="bootom-sec"> <form class="hook-form" action='https://feedburner.google.com/fb/a/mailverify?uri=TechLekhak' method='post' onsubmit='window.open (&apos;https://feedburner.google.com/fb/a/mailverify?uri=12c55&apos;, &apos;popupwindow&apos;, &apos;scrollbars=yes,width=550,height=520&apos;);return true' target='popupwindow'> <input placeholder="Enter your email id" name="email" type="text" /> <input value="12c55" name="uri" type="hidden" /> <input value="en_US" name="loc" type="hidden" /><button class="subs-btn">Subscribe Now</button> </form> </div> </div> </div> 

You are matching the wrong thing with your query selector. 您将错误的内容与查询选择器匹配。 You are trying to get an element with a "close" id but you have none in your code. 您正在尝试获取具有“关闭” ID的元素,但是您的代码中没有任何元素。

This could be arranged by adding id="close" to thr tag you want to trigger the click function you defined 可以通过在要触发定义的点击功能的thr标签中添加id="close"来进行安排

Example : 范例:

<div id="close" class="hook-close-btn sprite-img">Close</div>

Same is happening to the queryselector that matches the item you want to close... It doesn't exist and you will need to adjust it 与您要关闭的项目相匹配的queryselector发生了同样的事情...它不存在,您需要对其进行调整

You can change the id="toggle" around the page to see that those elements are the ones that get open and closed 您可以在页面周围更改id="toggle"以查看这些元素是打开还是关闭的元素

Here is a snippet 这是一个片段

 var toggler = document.querySelector("#close"); toggler.addEventListener("click", function() { var toggleElement = document.querySelector("#toggle"); if (!toggleElement.style.display || toggleElement.style.display === "block") { toggleElement.style.display = "none"; } else { toggleElement.style.display = "block"; } }); 
 <div class="hook-section js-common-hook"> <div class="hook-content small-hook-show"> <button id="close" class="hook-close-btn sprite-img">Close</button> <div class="top-sec clearfix"> <div class="left-sec sprite-img"> </div> <div class="right-sec"> <h5>Email Newsletter</h5> <p>The best of technology updates from India and around the world</p> </div> </div> <div class="bootom-sec"> <form id="toggle" class="hook-form" action='https://feedburner.google.com/fb/a/mailverify?uri=TechLekhak' method='post' onsubmit='window.open (&apos;https://feedburner.google.com/fb/a/mailverify?uri=12c55&apos;, &apos;popupwindow&apos;, &apos;scrollbars=yes,width=550,height=520&apos;);return true' target='popupwindow'> <input placeholder="Enter your email id" name="email" type="text" /> <input value="12c55" name="uri" type="hidden" /> <input value="en_US" name="loc" type="hidden" /><button class="subs-btn">Subscribe Now</button> </form> </div> </div> </div> 

what element are you targeting with this? 您打算以此为目标?

var t = document.querySelector(div.getAttribute("data-target"));

Your code should be: 您的代码应为:

<div id="popup" class="hook-section js-common-hook">    //your box

<div id="close" class="hook-close-btn sprite-img">Close</div>      // your button

then: 然后:

var button = document.querySelector("#close");
button.addEventListener("click", function() {
  var t = document.querySelector("#popup"));
  t.style.display = "none";
});

Basically you need to fix your elements' names. 基本上,您需要修复元素的名称。 Let me know if it works :) 让我知道它是否有效:)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM