简体   繁体   English

将.click()添加到动态创建的标签页

[英]Add .click() to dynamically created tab

I'm attempting to add a .click() to a tab that I add dynamically. 我试图将.click()添加到动态添加的标签中。

Code so far: 到目前为止的代码:

var newtabid = "#privChatArea" + chatmessage.SenderID;

$("#tabs").tabs("add", newtabid, "<span style=\"color: red;\">" + chatmessage.SenderNick + "</span>");

I can't seem to figure out how to reference the Tab-button, as the only element I am actually giving an ID is the <div id="privChatArea[id]"></div> 我似乎无法弄清楚如何引用Tab按钮,因为我实际上给ID的唯一元素是<div id =“ privChatArea [id]”> </ div>

Anyone know how to do this? 有人知道怎么做吗?

EDIT: Clarification 编辑:澄清

The tabs consist of 选项卡包括

   <div id="tabs">
    <ul id="tabscontainer">
        <li><a href="#chatroom1" id="_chatroom1"><span>test1</span></a></li>
        <li><a href="#chatroom2" id="_chatroom2"><span>test2</span></a></li>
        <li><a href="#chatroom3" id="_chatroom3"><span>test3</span></a></li>
    </ul>
    <div id="chatroom1" style="max-height: 400px; height: 400px; width: 100%; overflow-y: scroll;">
    </div>
    <div id="chatroom2" style="max-height: 400px; height: 400px; width: 100%; overflow-y: scroll;">
    </div>
    <div id="chatroom3" style="max-height: 400px; height: 400px; width: 100%; overflow-y: scroll;">
    </div>

I'm having trouble getting a reference to the id of the which is created when the tab is also created. 我在获取也创建选项卡时创建的ID的引用时遇到了麻烦。 This ID is not specified when using the .tabs("add") 使用.tabs(“ add”)时未指定此ID

Cheers! 干杯!

use "live" to bind events for dynamically created elements. 使用“实时”为动态创建的元素绑定事件。 You can choose one kind of selectors (may be a class etc) for all your tabs and bind events with live on your $(document).ready() 您可以为所有选项卡选择一种选择器(可能是类,等等),并在$(document).ready()中将事件与实时绑定。

http://docs.jquery.com/Events/live http://docs.jquery.com/Events/live

Edit: After reading your question again, thought I should clarify. 编辑:再次阅读您的问题后,我想澄清一下。 You should bind the live event to the "span" which you are creating 您应该将实时事件绑定到正在创建的“ span”上

Should be as simple as: 应该很简单:

$("#tabs span.private-chat-area").live("click", function(e) {
    var senderId = $(this).attr("rel");
    ...
});

var newtabid = "#privChatArea" + chatmessage.SenderID;

$("#tabs").tabs("add", newtabid, "<span style=\"color: red;\" class=\"private-chat-area\" rel=\"" + chatmessage.SenderID + "\">" + chatmessage.SenderNick + "</span>");

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

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