简体   繁体   English

动态填充用户

[英]Populating users dynamically

I creating a chat system with the Facebook layout where you have the name listed on the right and the chat boxes popup when the names on the right gets clicked. 我创建了一个具有Facebook布局的聊天系统,其中您的名称在右侧列出,并且在单击右侧的名称时会弹出聊天框。 The system currently works where the list on the right with users are set by me gor eg 该系统当前可以在右侧用用户设置列表的方式运行,例如

<div class="chat-sidebar">
    <div class="sidebar-name">
        <!-- Pass username and display name to register popup -->
        <a href="javascript:register_popup('Jodaine', 'Jodaine');">
            <img width="30" height="30" src="@Url.Content("~/Images/Jodaine.PNG")"/>
            <span>Jodaine</span>
        </a>
    </div>
</div> 

This works perfectly well but I would Like to populate the users dynamically. 这工作得很好,但是我想动态地填充用户。 So when a user signs in. 因此,当用户登录时。

I have this function which does the same thing through the script but when I call the function it goes on top of the div that is already there. 我有这个函数,可以通过脚本执行相同的操作,但是当我调用该函数时,它会位于已经存在的div之上。 so the names overlap. 因此名称重叠。

    function populatesidebar() {
        ccc = ccc + 1;
        var element = '';
        element = element + '<div class="chat-sidebar">';

        element = element + '<div class="sidebar-name"><a href="javascript:register_popup(\'Jodaine2\', \'Jodaine2\');"><img width="30" height="30" src="@Url.Content("~/Images/Jodaine.PNG")"/><span>Jodaine'+ccc+'</span></a></div></div>';

        document.getElementsByTagName("body")[0].innerHTML = document.getElementsByTagName("body")[0].innerHTML + element;
    }

this function is called every three seconds using: 使用以下命令每三秒钟调用一次此函数:

$(document).ready(function () {
    //let the code run every second
    setInterval(function() {
        populatesidebar();
    },3000); //1000 = 1 seconds
});

I have added: 我已经添加了:

document.getElementsByClassName("chat-sidebar").remove();

at the beginning of the populatesiderbar function but then nothing shows. 在populatesiderbar函数的开头,但随后没有任何显示。

If I can assume the chat-sidebar element is in your initial HTML: 如果我可以假设chat-sidebar元素位于您的初始HTML中:

function populatesidebar() {
    ccc = ccc + 1;

    var sidebarNameHtml = '<div class="sidebar-name"><a href="javascript:register_popup(\'Jodaine2\', \'Jodaine2\');"><img width="30" height="30" src="@Url.Content("~/Images/Jodaine.PNG")"/><span>Jodaine'+ccc+'</span></a></div>';

    $('.chat-sidebar').html(sidebarNameHtml);

}

Here I'm using jQuery's html() method to set the content for chat-sidebar , overwriting any existing content. 在这里,我使用jQuery的html()方法设置chat-sidebar的内容,并覆盖所有现有内容。 You have jQuery in place already so take advantage of it :) 您已经安装了jQuery,因此可以利用它:)

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

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