In jquery-ui 1.9, how do you create new tabs dynamically?

前端 未结 5 1277
情话喂你
情话喂你 2020-12-13 14:51

According to the upgrade guide for jquery-ui 1.9 tabs - http://jqueryui.com/upgrade-guide/1.9/#deprecated-add-and-remove-methods-and-events-use-refresh-method - when adding

5条回答
  •  -上瘾入骨i
    2020-12-13 15:22

    It is annoying that the tabs "add" was removed, however jQuery is very easy to extend. Just add your own addTab method.

    e.g. Something like this:

    // jQuery extension method
    $.fn.addTab = function (name) {
        $('ul', this).append('
  • ' + name + '
  • '); $(this).append("
    "); $(this).tabs("refresh"); };

    And simply use like this:

    $('#tabs').addTab("NewTab");
    $('#tabs').addTab("Another NewTab");
    $('#tabs').addTab("etc");
    

    JSFiddle: http://jsfiddle.net/TrueBlueAussie/AJDLt/315/

    As suggested by @Andy, you need to make it use .first() if you nest tabs in your interface:

    e.g. Something like this:

    // jQuery extension method
    $.fn.addTab = function (name) {
        $('ul', this).first().append('
  • ' + name + '
  • '); $(this).append("
    "); $(this).tabs("refresh"); };

提交回复
热议问题