简体   繁体   English

addEventListener()在小屏幕上不起作用

[英]addEventListener() not working on small screens

I've got a table on my wordpress, with a button on each row, which creates a small form when you click on it ( on this page ). 我的wordpress上有一张桌子,每一行都有一个按钮,当您单击它时( 在此页面上 )会创建一个小表格。 It's working perfectly fine, except when you change your browser's width to <768px, then nothing happens. 它工作得很好,除非您将浏览器的宽度更改为<768px,然后什么也没有发生。

This is my main call : 这是我的主要电话:

var buttons = [];
if (jQuery(window).width() > 768)
    buttons = document.querySelectorAll('td button');
else
    buttons = document.querySelectorAll('div button');

/*
 Add a click event to all buttons inside the table
 */
for (var i = 0; i < buttons.length; i++) {
    buttons[i].addEventListener('click', clickCheck);
}

On small screens, the table elements become divs, so I had to define the buttons array differently. 在小屏幕上,表元素变为div,因此我必须以不同的方式定义buttons数组。
Now this is the clickCheck() function : 现在这是clickCheck()函数:

function clickCheck(e) {
    console.log('ok');
    // if first click on the button
    if (e.target.textContent === 'Inscription') {
        // Cleaning an eventual earlier click on another button
        for (var j = 0; j < buttons.length; j++) {
            if (buttons[j].textContent === 'Fermer') {
                buttons[j].parentNode.removeChild(buttons[j].parentNode.firstChild);
                buttons[j].textContent = 'Inscription';
            }
        }

        // Popping the form
        formPop(e);
        e.target.textContent = 'Fermer';
    }
    // if form submission
    else {
        // Depopping the form
        formDepop(e);
        e.target.textContent = 'Inscription';
    }
}

On screens smaller than 768px, the 'ok' isn't appearing in the console, so the event isn't read. 在小于768像素的屏幕上,控制台中未显示“确定”,因此不会读取该事件。 I've checked and my buttons array is fine even on small screens, so the problem has to come from addEventListener() . 我已经检查过,即使在小屏幕上,我的buttons数组也很好,所以问题必须来自addEventListener()

Any clue ? 有什么线索吗? :) :)

Your first code is only being run once, right? 您的第一个代码只运行一次,对吧? I bet it works on small screens if you load the page small, but not if you load it wider than 768 and then resize down to small. 我敢打赌,如果您将页面加载得很小,那么它可以在小屏幕上运行,但是如果您加载的页面宽于768,然后将其缩小到较小的尺寸,则无法运行。

Basically what appears to be happening is that the event listeners are being added when the page is greater than 768 wide, but the selection of what to add them to is based on exactly that. 基本上,似乎发生的是,当页面大于768宽,但什么增加他们的选择,以基于正是该事件侦听器被添加。

There's a lot of code running in your page so I don't know the root case, but here's the general problem. 您的页面中运行着很多代码,所以我不知道根本原因,但这是普遍的问题。

When you do this at startup: 在启动时执行此操作:

 buttons = document.querySelectorAll('div button');

You are getting a different array of button elements than what ends up visible in your page. 您获得的按钮元素数组与页面中可见的按钮元素数组不同。 You can physically compare the original buttons array with what happens when you run a new document.querySelectorAll('div button') at the time of the click (I did so in the debugger). 您可以将原始的buttons数组与单击时运行新document.querySelectorAll('div button')时发生的情况进行物理比较(我在调试器中是这样做的)。 The first will contain 10 buttons, the second will contain 20 buttons. 第一个包含10个按钮,第二个包含20个按钮。 I think some code in your page is generating dynamic content AFTER you hook up the event handlers and what you're seeing and interacting with in the page is the dynamic content that was created after you ran your code to add click handlers so the visible content has no event handlers on it. 我认为页面中的一些代码会在生成事件处理程序后生成动态内容,并且您在页面中看到并与之交互的是在运行代码以添加点击处理程序后创建的动态内容,因此可见内容没有事件处理程序。

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

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