Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 添加简单事件侦听器_Javascript_Html_Event Handling_Addeventlistener - Fatal编程技术网

Javascript 添加简单事件侦听器

Javascript 添加简单事件侦听器,javascript,html,event-handling,addeventlistener,Javascript,Html,Event Handling,Addeventlistener,我需要添加一个简单的事件侦听器。即使在使用console.log进行测试时,它也不起作用。这是我的档案: index.html <!DOCTYPE html> <html class="no-js"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>CHAT</

我需要添加一个简单的事件侦听器。即使在使用console.log进行测试时,它也不起作用。这是我的档案:

index.html

<!DOCTYPE html>
<html class="no-js">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>CHAT</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
    <form>
        <input id='input' cols="30" rows="10"></input>
        <button id='send'>Send</button>
    </form>
    <div id='output' style='background-color: aqua;'> 1 </div>
</body>


<script src='https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.2/socket.io.js'></script>
<script src="./scripts/sockets.js"></script>
</script>

</html>

我还想承认这条路是正确的。“发送”已在控制台中发布,但“添加了事件侦听器”未发布。

我发现在更改为的情况下,一切正常,这可能意味着什么?。如果单击,则不会记录任何内容;如果双击,则会记录相应的消息,但会很快从chrome控制台中删除。当您按下“发送”按钮时,页面是否会重新加载?因为它在表单中,可以作为提交按钮。我发现在更改为的情况下,一切都正常工作,这意味着什么?。如果单击,则不会记录任何内容;如果双击,则会记录相应的消息,但会很快从chrome控制台中删除。当您按下“发送”按钮时,页面是否会重新加载?因为它位于表单中,可以作为提交按钮。
var socket = io.connect('http://localhost:3000/chat');
var input = document.getElementById('input');
var send = document.getElementById('send');
var output = document.getElementById('output');
console.log('send');
send.addEventListener('click', () => {
    console.log('event listener added');
    /*socket.emit('sending', {
        input: input.value
    });
    input.value = '';*/
});