【问题标题】:when opening new tabs, it should open the new tab diretly in jquery ui tabs打开新标签时,应该直接在 jquery ui 标签中打开新标签
【发布时间】:2016-04-04 07:29:26
【问题描述】:

我正在使用 jquery-UI 创建动态选项卡面板。当我单击添加选项卡按钮时,正在创建新选项卡。但该选项卡面板没有直接打开。当单击该选项卡面板时,只有它正在打开。

 $(function() {
var tabTitle = $( "#tab_title" ),
  tabContent = $( "#tab_content" ),
  tabTemplate = "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close' role='presentation'>Remove Tab</span></li>",
  tabCounter = 2;

var tabs = $( "#tabs" ).tabs();

// modal dialog init: custom buttons and a "close" callback resetting the form inside
var dialog = $( "#dialog" ).dialog({
  autoOpen: false,
  modal: true,
  buttons: {
    Add: function() {
      addTab();
      $( this ).dialog( "close" );
    },
    Cancel: function() {
      $( this ).dialog( "close" );
    }
  },
  close: function() {
    form[ 0 ].reset();
  }
});

// addTab form: calls addTab function on submit and closes the dialog
var form = dialog.find( "form" ).submit(function( event ) {
  addTab();
  dialog.dialog( "close" );
  event.preventDefault();
});

// actual addTab function: adds new tab using the input from the form above
function addTab() {
  var label = tabTitle.val() || "Tab " + tabCounter,
    id = "tabs-" + tabCounter,
    li = $( tabTemplate.replace( /#\{href\}/g, "#" + id ).replace( /#\{label\}/g, label ) ),
    tabContentHtml = tabContent.val() || "Tab " + tabCounter + " content.";

  tabs.find( ".ui-tabs-nav" ).append( li );
  tabs.append( "<div id='" + id + "'><p>" + tabContentHtml + "</p></div>" );
  tabs.tabs( "refresh" );
  tabCounter++;
}

// addTab button: just opens the dialog
$( "#add_tab" )
  .button()
  .click(function() {
    dialog.dialog( "open" );
  });

// close icon: removing the tab on click
tabs.delegate( "span.ui-icon-close", "click", function() {
  var panelId = $( this ).closest( "li" ).remove().attr( "aria-controls" );
  $( "#" + panelId ).remove();
  tabs.tabs( "refresh" );
});

tabs.bind( "keyup", function( event ) {
  if ( event.altKey && event.keyCode === $.ui.keyCode.BACKSPACE ) {
    var panelId = tabs.find( ".ui-tabs-active" ).remove().attr( "aria-controls" );
    $( "#" + panelId ).remove();
    tabs.tabs( "refresh" );
  }
});

});

这是我在我的网站中使用的 jquery UI 选项卡面板的 URL [http://jqueryui.com/tabs/#manipulation].

如何直接打开这个新增的标签页?

【问题讨论】:

  • 在打开选项卡时有两件事需要修改第一个选项卡类需要添加新创建的选项卡并从当前活动选项卡中删除活动类并在选项卡内容中应用 css,例如在活动时显示:块跨度>

标签: javascript jquery html css jquery-ui


【解决方案1】:

在 addTab() 函数中添加以下代码

    var lastChildIndex = $(".ui-tabs-nav").children().length - 1;
    $("div#tabs").tabs({active: lastChildIndex});

它的作用是获取选项卡列表的最后一个子项(您刚刚添加的那个)的索引并将其设置为活动状态。

示例:http://jsfiddle.net/ujUu2/818/

编辑:鉴于您的代码中已经有一个 tabCounter,您可以将其用作设置最后一个选项卡处于活动状态的索引,而不是我建议的 var lastChildIndex。

【讨论】:

  • 我已经添加了上面的代码。现在它工作正常。谢谢
猜你喜欢
  • 2022-11-18
  • 2016-07-16
  • 1970-01-01
  • 2011-09-19
  • 2011-12-13
  • 1970-01-01
  • 2023-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多