Javascript 我调用了Socket.io函数和uncaughttypeerror:undefined不是函数
我正在制作socket.io聊天应用程序,但在javascript方面遇到了麻烦 这是我的javascript文件(客户端) “socket.emit('chat message',$('.chat_form').firstChild().val());”带来 未捕获类型错误:未定义不是函数Javascript 我调用了Socket.io函数和uncaughttypeerror:undefined不是函数,javascript,jquery,node.js,sockets,socket.io,Javascript,Jquery,Node.js,Sockets,Socket.io,我正在制作socket.io聊天应用程序,但在javascript方面遇到了麻烦 这是我的javascript文件(客户端) “socket.emit('chat message',$('.chat_form').firstChild().val());”带来 未捕获类型错误:未定义不是函数 我认为这是关于javascript的问题。有人能帮我吗 这是你的HTML。可能未找到$('.chat_form')或firstChild()。感谢您的回复。我认为在运行时生成元素(append)会带来很多问
我认为这是关于javascript的问题。有人能帮我吗 这是你的HTML。可能未找到
$('.chat_form')
或firstChild()
。感谢您的回复。我认为在运行时生成元素(append)会带来很多问题。。你能建议其他方式吗@Vasiliyborovyak()用于前端。这是用于构建UI的最先进的简单库。你是最棒的!!非常感谢@VasiliyBorovyak
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function() {
// make a new chat div
$("#topic_button").click(function(e) {
e.preventDefault()
var domElement = $('<div class="content"><div class="messages"><ul class = "other_message" class="title"><h3>'+$("#m").val()+'</h3></ul>'+'상대방'+'<div class="my_message">'+'내메시지' +'</div></div><div class="type_div"><form action="" class="chat_form" onsubmit="event.preventDefault();"><input autocomplete="off" /><button class="chat_button">Send</button></form></div></div>');
$("#content").after(domElement);
});
// create a new category
$("#topic_button").click(function(e){
var $item = $('<div class="item_div"><li>' + $("#m").val() + '</li><button class="item_button">X</button><div></div></div>')
$('#category').append($item);
$('#m').val('');
});
// delete category
$("#category").on("click", ".item_button", function () {
var title = $(this).prev().html();
$( "h3:contains("+title+")" ).parents(".content").remove();
$(this).parent().slideUp();
});
var socket = io();
$('body').on("submit", ".chat_form", function(){
socket.emit('chat message', $('.chat_form').firstChild().val());
$('.chat_form').firstChild().val('');
return false;
});
socket.on('chat message', function(msg, reply){
$('.other_message').append($('<li>').text(reply + ': ' + msg));
});
socket.on('test channel', function(msg, sender){
$('.my_message').append($('<li>').text(sender + ': ' + msg));
});
});
</script>
$('body').on("submit", ".chat_form", function(){
socket.emit('chat message', $('.chat_form').firstChild().val());
$('.chat_form').firstChild().val('');
return false;
});