2017-08-26 7 views
-1

私はnode.jsとsocket.ioを学習していますが、この例を使用しています。すべてが機能していますが、 すべてのjqueryのアペンドがロードされているJQueryは非常に単純なケースでは動作しません。

$("#messages").append("<p>someone said: </p>"); 

のjQueryのような、でもシンプルなものを作業されていません。

$.fn.jquery 
"1.11.1" 

これは全体のindex.htmlです:

<!doctype html> 
<html ng-app="chatapp"> 
    <head> 
    <title>Socket.IO chat app</title> 
    <style> 
     * { margin: 0; padding: 0; box-sizing: border-box; } 
     body { font: 13px Helvetica, Arial; } 
     form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; } 
     form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; } 
     form button { width: 9%; background: rgb(10, 24, 255); border: none; padding: 10px; } 
     #messages { list-style-type: none; margin: 0; padding: 0; } 
     #messages li { padding: 5px 10px; } 
     #messages li:nth-child(odd) { background: #eee; } 
     </style> 
    </head> 
    <body> 
    <ul id=messages">Messages: </ul> 
    <form action=""> 
     <input id="m" autocomplete="off" /> 
     <button>Send</button> 
    </form> 
<script src="/socket.io/socket.io.js" type="text/javascript"></script> 
<script src="https://code.jquery.com/jquery-1.11.1.js" type="text/javascript"></script> 
<script> $(document).ready(function(){ $("#messages").append("<p>Initializing... </p>"); });</script> 
<script type="text/javascript"> 
$(function() { 
    var socket = io(); 
    $('form').submit(function(){ 
     socket.emit('chat message', $('#m').val()); 
     $('#m').val(''); 
     return false; 
    }); 
    socket.on('chat message', function(msg){ 
     console.log('incoming: ' + msg); 
     $("#messages").append("<p>someone said: </p>"); 
     console.log('second append '); 
     $('#messages').append($('<li></li>').text("hola")); 
    }); 
socket.on('connection', function() { 
    console.log("client connected"); 
}); 

socket.on('connect_error', function(err) { 
    console.log("client connect_error: ", err); 
}); 

socket.on('connect_timeout', function(err) { 
    console.log("client connect_timeout: ", err); 
}); 
}); 
</script> 
    </body> 
</html> 

参照してください。その

<script> $(document).ready(function(){$("#messages").append("p>Initializing... </p>"); });</script> 

も表示されていません。

+0

あなたのpタグにあなたの足跡がありません。 – Keith

答えて

0

奇妙です。私は通常

$('<p>someone said:</p>').appendTo('#messages'); 

そして、他の回答でPhani Kumar氏によって指摘などとして、あなたはid属性に1 "を欠場することを書きます。それはid="messages"でなければなりません。

+0

あなたは 'appendTo'を意味すると思いますか? –

+0

はい、回答を更新します –

関連する問題