简体   繁体   English

单击时addEventListener不起作用

[英]addEventListener is not working when on click

Hi i am trying to use addEventlistener on an event firing.But it is not working. 嗨,我正在尝试在事件触发上使用addEventlistener。但是它不起作用。 When i click on header_search button it works fine and log on console "clicked" and a window opens . 当我单击header_search按钮时,它工作正常,并在控制台上“单击”并打开一个窗口。 On the opened window there is a close button which id is "search-close" . 在打开的窗口中,有一个关闭按钮,其ID为“ search-close”。 and when i press on the button that does not work.Even it does not console log "clicked". 当我按下不起作用的按钮时。即使它没有控制台日志“单击”。 Could anyone help? 有人可以帮忙吗? Is that a browser problem? 那是浏览器问题吗?

var search = document.getElementById('header_search');
var close = document.getElementById('search-close');

console.log(search);
console.log(close);

search.addEventListener('click', function(event){
    console.log("clicked");
    event.preventDefault();
    showFullScreen();
    document.getElementById('search-top').focus();
});

close.addEventListener('click', function(event){
    event.preventDefault();
    console.log("clicked");
    searchClose();
});

function showFullScreen(){
    $('html').prepend('<div class="full-screen-search" />');
    var search_form = document.getElementById('search-header-form').innerHTML;
    $(".full-screen-search").html(search_form);
    $(".full-screen-search").fadeIn(1000);
};

function searchClose(){
    $(".full-screen-search").fadeOut(200);
    $(".full-screen-search").remove();

};

If you are using jQuery, don't use addEventListener and getElementById . 如果您使用的是jQuery,请不要使用addEventListenergetElementById That kind of defeats the "more with less" benefits of jQuery. 这种方式击败了jQuery的“事半功倍”优势。 Use .click(function(){...}) or .on('click', function(){...}) and $('#idname') 使用.click(function(){...}).on('click', function(){...})$('#idname')

You are adding your window dynamically, so the close button is not the one you originally found with getElementById('search-close') . 您正在动态添加窗口,因此关闭按钮不是最初通过getElementById('search-close')找到的按钮。

Use a jQuery delegated event handler attached to a non-changing ancestor element 使用附加到不变祖先元素的jQuery 委托事件处理程序

eg 例如

 $(document).on('click', '#search-close', function(){...

You cannot have duplicate IDs on a page, or only the first can be found. 您在页面上不能有重复的ID,或者只能找到第一个。 Suggest you change to using classes instead: 建议您改为使用类:

 $(document).on('click', '.search-close', function(){...

Delegated events work by listening for events (eg click) to bubble up to a non-changing ancestor element. 委派的事件通过侦听事件(例如,单击)冒泡至不变的祖先元素来工作。 document is the best default of nothing else is closer. document是最好的默认值,别无选择。 It then applies the jQuery selector at event time, so the target does not need to exist when the code was registered. 然后,它在事件发生时应用jQuery选择器,因此在注册代码时不需要存在目标。

If you can also provide your page HTML, I will provide a full example of how to better write your code with jQuery :) 如果您还可以提供页面HTML,我将提供一个完整的示例,说明如何使用jQuery更好地编写代码 :)

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

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