我试图通过以下教程https://www.youtube.com/watch?v=uJC8A_7VZOA创建一个flacks -socketio聊天应用程序程序
我想打印"加入了聊天"或者div
chat.html javascript
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js"></script>
<script>
const socket = io.connect("http://127.0.0.1:5000");
socket.on('connect', function(){
socket.emit('join_room',{
username: "{{ username }}",
room: "{{ room}}"
})
})
socket.on('join_room_announcement', function (data) {
console.log(data);
if (data.username !== "{{ username }}") {
const newNode = document.createElement('div');
newNode.innerHTML = `<b>${data.username}</b> has joined the room`;
document.getElementsByName('messages').append(newNode);
}
});
</script>
我不包括css代码,因为我想它不是那么必要。
这是我的app.py
@app.route('/chat')
def chat():
username = request.args.get('username')
room = request.args.get('room')
if username and room:
return render_template('chat.html',username=username,room=room)
else:
return "Bad/incomplete Request Errror Code 405/400"
@socketio.on('join_room')
def handle_join_room_event(data):
print("{} has joined room {}".format(data["username"],data["room"]))
# logging.info("{} has joined room {}".format(data["username"],data["room"]))
join_room(data['room'])
socketio.emit('join_room_announcement', data)
if __name__ == '__main__':
socketio.run(app,debug=True)
我得到错误Uncaught TypeError: document.getElementsByName(…)。Append不是一个函数
我附上一个截图供参考:输入图片描述
原来我是个白痴。我必须改变
<div id = messages
>来<div id =messages>