Javascript 新建聊天窗口

Javascript 新建聊天窗口,javascript,html,Javascript,Html,我有一次聊天(到目前为止没有任何设计) 我的问题: 聊天在新窗口中不起作用,但在“索引窗口”中起作用。 我对javascript一无所知,我不知道问题出在哪里。 我想这是因为身份证或其他原因。 有人能帮我吗?我可以在新窗口聊天吗? 谢谢:)新页面的输入还没有事件,请绑定它的事件 再加上这个 openChat: function(){ win =top.consoleRef=window.open('','myconsole', 'width=350,height=250'

我有一次聊天(到目前为止没有任何设计)


我的问题:

聊天在新窗口中不起作用,但在“索引窗口”中起作用。 我对javascript一无所知,我不知道问题出在哪里。 我想这是因为身份证或其他原因。 有人能帮我吗?我可以在新窗口聊天吗?
谢谢:)

新页面的输入还没有事件,请绑定它的事件

再加上这个

openChat: function(){
    win =top.consoleRef=window.open('','myconsole',
    'width=350,height=250'
    +',menubar=0'
    +',toolbar=1'
    +',status=0'
    +',scrollbars=1'
    +',resizable=1')
    chat = document.getElementById("chatHtml").innerHTML;
    win.document.write(chat);

    win.document.getElementById('input-text-chat').onkeyup = function(e) {
        if (e.keyCode != 13) return;
        // removing trailing/leading whitespace
     //   this.value = this.value.replace(/^\s+|\s+$/g, '');
        if (!this.value.length) return
        connection.send(this.value);
        console.log(connection.send);
        console.log(this.value);
        appendDIV(this.value);
        this.value = '';
    };

}
之后


另外,如果您将其命名为该事件以减少代码,则效果更好

新页面的输入尚未包含事件,因此请绑定它的事件

再加上这个

openChat: function(){
    win =top.consoleRef=window.open('','myconsole',
    'width=350,height=250'
    +',menubar=0'
    +',toolbar=1'
    +',status=0'
    +',scrollbars=1'
    +',resizable=1')
    chat = document.getElementById("chatHtml").innerHTML;
    win.document.write(chat);

    win.document.getElementById('input-text-chat').onkeyup = function(e) {
        if (e.keyCode != 13) return;
        // removing trailing/leading whitespace
     //   this.value = this.value.replace(/^\s+|\s+$/g, '');
        if (!this.value.length) return
        connection.send(this.value);
        console.log(connection.send);
        console.log(this.value);
        appendDIV(this.value);
        this.value = '';
    };

}
之后



另外,如果您将它命名为事件,以减少代码的数量,这也会更好

它不起作用,因为它没有绑定事件。您能更准确地解释它吗?我对这个很陌生,我甚至不知道javascript中的绑定是什么,我指的是事件。它会在某些事情发生时读取它(触摸)您的元素示例单击、悬停、鼠标向上等。但是它可以工作,我只需要在新窗口中输入/输出框的ID。我不知道我是否能用事件来实现它。它不起作用,因为它没有绑定事件。是的,你能更准确地解释一下吗?我对这个很陌生,我甚至不知道javascript中的绑定是什么,我指的是事件。它会在某些事情发生时读取它(触摸)您的元素示例单击、悬停、鼠标向上等。但是它可以工作,我只需要在新窗口中输入/输出框的ID。我不知道我是否能通过事件实现它。我仍然无法在新窗口和行中写入:win.document.write(chatContainer.innerHTML);是错误的,那只是一次尝试,忘了删除它。你能截图你的js整个js文件吗?只是我告诉你添加的那个让我们继续在聊天室中。我仍然无法在新窗口中写入行:win.document.write(chatContainer.innerHTML);是错误的,那只是一次尝试,忘了删除。你能截图你的js吗?整个js文件?就是我告诉你要添加的那个让我们继续聊天
document.getElementById('input-text-chat').onkeyup = function(e) {
    if (e.keyCode != 13) return;
    // removing trailing/leading whitespace
    //   this.value = this.value.replace(/^\s+|\s+$/g, '');
    if (!this.value.length) return
    connection.send(this.value);
    console.log(connection.send);
    console.log(this.value);
    appendDIV(this.value);
    this.value = '';
};
var chatContainer = document.querySelector('.chat-output');

function appendDIV(event) {
    var div = document.createElement('div');
    div.innerHTML = event.data || event;
    chatContainer.insertBefore(div, chatContainer.firstChild);
    div.tabIndex = 0;
    div.focus();
    document.getElementById('input-text-chat').focus();
    win.document.write(chatContainer.innerHTML);
}
openChat: function(){
    win =top.consoleRef=window.open('','myconsole',
    'width=350,height=250'
    +',menubar=0'
    +',toolbar=1'
    +',status=0'
    +',scrollbars=1'
    +',resizable=1')
    chat = document.getElementById("chatHtml").innerHTML;
    win.document.write(chat);

    win.document.getElementById('input-text-chat').onkeyup = function(e) {
        if (e.keyCode != 13) return;
        // removing trailing/leading whitespace
     //   this.value = this.value.replace(/^\s+|\s+$/g, '');
        if (!this.value.length) return
        connection.send(this.value);
        console.log(connection.send);
        console.log(this.value);
        appendDIV(this.value);
        this.value = '';
    };

}
win.document.write(chatContainer.innerHTML);