Javascript读取一次然后部分忽略?

问题描述 投票:0回答:1

以下JS位于HTML下方的脚本标记中。当页面最初加载时,该文件似乎工作。创建一个频道,当我点击它时,会出现console.log语句,表明它是可操作的。然后我成功添加了一个频道。但是,当我点击其他频道时,没有任何反应。我从未到达页面底部的.forEachclick eventListener - 专为多个通道按钮构建的案例。我尝试了各种配置并研究了查询选择器和forEach。我哪里错了?

(function() {


    //connect to websocket: copy must be inside request.onload for chat room to register user entry
    let socket = io.connect(location.protocol + '//' + document.domain + ':' + location.port);

    if (socket) 
        console.log("Socket connected!");

    socket.on('connect', () => {
        // track number of channels and channel names
        let channelCount = 1;
        let channelList = ["Lobby"];

        // create & display new channels added by user
        let createChannelBtn = document.querySelector('#chanlBtn');

        createChannelBtn.addEventListener('click', function(e) {

            //e.preventDefault();

            let newChannel = document.querySelector('#chanl').value;

            // add a channel if not a duplicate
            if (channelList.indexOf(newChannel) > -1) {

                document.querySelector('#chanl').value = '';

            } else {
                channelCount += 1;
                channelList.push(newChannel);

                console.log(channelList);
                console.log(channelCount);
                let textnode = document.createTextNode(newChannel);
                let node = document.createElement('button');
                node.className += 'dropdown-item';
                node.setAttribute('type', 'button');
                node.setAttribute('id', 'selectChannel');
                node.setAttribute('data-channel-', 'newChannel');
                node.appendChild(textnode);
                document.querySelector('#chanlMenu').appendChild(node);
                document.querySelector('#chanl').value = '';

            }

        });

        if (channelCount == 1) {
            var channel_button = document.querySelector('#selectChannel');
            console.log("channel 1 =" + channel_buttons);
            console.log("channelCount 1 =" + channelCount);
            channel_button.addEventListener('click', function() {
                let room = channel_button.dataset.channel;
                console.log("Inside lobby");
                socket.emit('join', {'username':localStorage.getItem('login'), 'room':room});
            });
        } 

        var channel_buttons = document.querySelectorAll('#selectChannel');


    HERE'S THE SECTION I NEVER REACH AFTER A CHANNEL BUTTON IS ADDED
            channel_buttons.forEach( channel_button => {
                channel_button.addEventListener('click', function() {
                let room = channel_button.dataset.channel;
                console.log("Sending 1 Button signal from Node List");
                //socket.emit('join', {'username':localStorage.getItem('login'), 'room':room});
            });

        });

    });

})()
javascript
1个回答
1
投票

看起来您在页面加载时将点击处理程序添加到频道按钮。

这个问题是按钮还不存在,所以你循环通过var channel_buttons = document.querySelectorAll('#selectChannel');创建的空列表

(尝试在该行之后放置一个console.log(channel_buttons);以查看列表包含的内容)

你需要在节点创建之后将事件监听器添加到节点(可能是你的node.addEventListener(...)行之后的node.appendChild(textnode);,或者使用event delegation在父节点上放置一个点击处理程序来处理所有新按钮的点击。

© www.soinside.com 2019 - 2024. All rights reserved.