繁体   English   中英

Javascript-如何动态添加新的div并向其添加数据

[英]Javascript - how to add new div dynamically and append data to it

我基本上是在SignalR的示例中工作-这是我的代码:

<!DOCTYPE html>
<html>
<head>
    <title>SignalR Simple Chat</title>
    <style type="text/css">
        .container {
            background-color: #99CCFF;
            border: thick solid #808080;
            padding: 20px;
            margin: 20px;
        }
    </style>

    <style type="text/css">
        .box {
            background-color: #0000ff;
            border: thick solid #008000;
            padding: 20px;
            margin: 20px;
        }
    </style>


</head>
<body>
<div class="container">
    <input type="text" id="message"/>
    <input type="button" id="sendmessage" value="Send"/>
    <input type="hidden" id="displayname" />
</div>

<div class="box" id="box1">
</div>


<!--Script references. -->
    <!--Reference the jQuery library. -->
    <script src="Scripts/jquery-1.6.4.min.js" "></script>
    <!--Reference the SignalR library. -->
    <script src="/Scripts/jquery.signalR-2.0.0.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="/signalr/hubs"></script>
    <!--Add script to update the page and send messages.-->
    <script type="text/javascript">
        $(function () {
            // Declare a proxy to reference the hub.
            var chat = $.connection.chatHub;
            // Create a function that the hub can call to broadcast messages.
            chat.client.broadcastMessage = function (name, message) {
                // Html encode display name and message.
                var encodedName = $('<div />').text(name).html();
                var encodedMsg = $('<div />').text(message).html();
                // Add the message to the page.    
                $('#box1').append('<li><strong>' + encodedName
                    + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
            };
            // Get the user name and store it to prepend to messages.
            $('#displayname').val(prompt('Enter your name:', ''));
            // Set initial focus to message input box.
            $('#message').focus();
            // Start the connection.
            $.connection.hub.start().done(function () {
                $('#sendmessage').click(function () {
                    // Call the Send method on the hub.
                    chat.server.send($('#displayname').val(), $('#message').val());
                    // Clear text box and reset focus for next comment.
                    $('#message').val('').focus();
                });
            });
        });
    </script>
</body>
</html>

在上面:

$('#box1').append('<li><strong>' + encodedName
                    + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');

一些数据添加到我的div中,它是一个带边框的矩形,每当我将某物写入文本框时,都会显示一个新数据,并且矩形会越来越大,但是我想在每个上方显示新的div(rectangle)我写新文字的时间。 怎么做?

我认为您的代码是正确的,但是您必须检查是否在div之前附加了书面文本。如果已经添加了,则您什么都不要做,但是如果不存在,则将文本附加到div。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM