javascript - Jquery-UI-Tab 的 ID 是否已更改?

标签 javascript jquery jquery-ui

我需要一些帮助...我做了以下事情:

<div class="tabs">
  <ul>
    <li><a id="tb-1" position=1 href="tabs-1"> A </a></li>
    <li><a id="tb-2" position=2 href="tabs-2"> B </a></li>
    <li><a id="new-tab" position=3 href="new-tab"> + </a></li>
  </ul>

  <div id="tabs-1"> ... some html...</div>
  <div id="tabs-2"> ... some html...</div>
  <div id="new-tab"></div>
</div>

所以...写了JS代码:

$(".tabs").tabs();

$("#new-tab").click( function( event ) {

    // new tab position
    var position = $(this).attr("position"); 

    // change ID of "new-tab" tab to "tb-X"
    $('#new-tab').attr("id", "tb-"+position);  

    // title of current tab
    $("#tb-"+position).html("NEW");

    // add <div> to the new tab 
    $(".tabs").append( "<div id=\"tabs-"+position+"\"></div>");

    // add some html to the tab (not realy the above code)
    $("#tabs-"+position).html( new_data );

    // add new tab to tabs              
    var url = "#tabs-"+(position*1+1);
    $(".tabs").tabs("add", url, "+");

    // insert ID "new-tab" to the added tab
    $('.tabs li a').eq(position).attr("id", "new-tab");

    ... some more stuffs

然后,结果看起来很完美......但是......

  • 点击[+]标签,添加一个新标签... ok

  • 再次点击 [+](这是最近添加的标签)没有任何反应

  • 点击 [NEW] 标签,它会再次创建一个新标签! ([NEW] 标签是旧的 [+] 标签)

检查代码,[+] 选项卡的 ID 已更改为新值......一切似乎都正常。

怎么了?

我希望我的英语很清楚!非常感谢!

最佳答案

这是一个更新。

既然我了解了 jQuery 选项卡控件,我认为这正是您想要的。顺便说一下,这个主意很酷。

JSFiddle:

http://jsfiddle.net/54WZH/5/

HTML

<div class="tabs">
  <ul>
    <li><a href="tabs-1"> A </a></li>
    <li><a href="tabs-2"> B </a></li>
  </ul>
</div>

JavaScript

var $tbs = $(".tabs");
$tbs.tabs().children("ul").append('<li><a id="new-tab" class="ui-state-default ui-corner-top"> + </a></li>');

$("#new-tab").click( function( event ) {

    //Need to remove our Plus button so it doesn't get counted when 
    //calling tabs("add",...)
    $(this).parent().remove();

    //Get the position of the new tab
    var pos = $tbs.find(" > ul > li:last").index() + 2; 

    //Insert the new tab & re-add the Plus button
    $tbs.tabs("add", "#ui-tabs-" + pos, "New").children("ul").append('<li><a id="new-tab" class="ui-state-default ui-corner-top"> + </a></li>');

    //Your dynamic tab content for this tab
    $("#ui-tabs-" + pos).html("Content-" + pos); 

    //Re-add the event for the Plus Button
    $("#new-tab").click(arguments.callee); 
});

关于javascript - Jquery-UI-Tab 的 ID 是否已更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8696884/

相关文章:

javascript - 如果选择了一个选项,则禁用了所有选择中的其他选定选项

c# - 此场景的 Web 窗体表格控件

javascript - 使用 jQuery UI "Accordion and Droppable"

javascript - JavaScript 中应该使用哪种方式进行 null 比较

javascript - 无法匹配任何路由 : ""

javascript - 在 html 或 JS 中有没有一种方法,当我将鼠标悬停在某些东西上时它会改变?

jquery droppable -> 避免多次删除同一对象

javascript - 从 POST 响应中删除评论标签

jquery - 将鼠标悬停在单独 div 中的列表项上时如何显示 "col-md-6"div

php - 完成后在ajax之后获取变量