属性返回到默认的外部函数

时间:2014-12-30 16:21:48

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

我正在使用socket.io和node.js.当我尝试更改代码中div的可见性时遇到问题。我调用一个函数,当代码在函数中时,它会执行我想要的操作,但第二个完成后它会返回到我设置的默认可见性。我已经看到这种情况发生在我的其他程序中,它让我发疯,因为我不明白为什么它在函数中起作用但后来又回来了。提前谢谢。

    <!doctype html>
<html>
  <head>

    <title>Websocket Sea Chat</title>

    <h1 align="center" id="titleHeading">Sea Chat</h1>

    <style>
        #messages
        {
            height: 500px;  
        }
        #chatWrap
        {
            float: left;
            border: 1px solid;  
        }
        #contentWrap
        {
            display: none;  
        }
    </style>
  </head>
  <body>
    <div id="userWrap">
        <form id="enterUser">
            Enter Username:
            <input id="username"></input>
            <input type="submit"></input>
        </form>
    </div>

    <div id="contentWrap">
        <div id="chatWrap">
            <div id="messages"></div>
            <form id="sendMsg">
                <input id="message" class="textBox" name="message" placeholder="Enter Message" cols="90" rows="10"></input>

                <br/>

                <button id="sendButton">Send</button>
            </form>
        </div>
        <div id="usersOnline"></div>
    </div>
    <script src="/socket.io/socket.io.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
    <script>
        function enterChat(data)
        {
            if(data)
            {
                $('#userWrap').hide();
                $('#contentWrap').show();
                alert("poop");  
            }
            else
            {
                alert("Username already taken!");   
            }
        }

        var socket = io();
        $('#enterUser').submit(function()
        {
            socket.emit('new user', $('#username').val(), function(data)
            {
                enterChat(data);
                /*if(data)
                {
                    $('#userWrap').hide();
                    $('#contentWrap').show();   
                }
                else
                {
                    alert("Username already taken!");   
                }*/
            });
            $('#username').val('');
        });

         $('#sendMsg').submit(function()
         {
            socket.emit('chat message', $('#message').val());
            $('#message').val('');
        });
        socket.on('chat message', function(msg)
        {
            $('#messages').append(msg + "<br/>");
        });
    </script>
  </body>
</html>

0 个答案:

没有答案