我调用了Socket.io函数和Uncaught TypeError:undefined不是函数

时间:2015-03-17 05:52:35

标签: javascript jquery node.js sockets socket.io

我正在制作socket.io聊天应用程序,我在javascript上遇到了麻烦。

这是我的javascript文件(客户端)

<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>

和At

$('body').on("submit", ".chat_form", function(){
socket.emit('chat message', $('.chat_form').firstChild().val());
$('.chat_form').firstChild().val('');
return false;
});

“socket.emit('chat message',$('。chat_form')。firstChild()。val());”带来 未捕获的TypeError:undefined不是函数

我觉得这是关于javascript的问题。有人能帮助我吗?

0 个答案:

没有答案