0

動的に jQuery タブを作成しています。各タブにIDを割り当てたい。

$("#addTab").live('click', function() {
    index++;
    var title = 'Tab.....  ' + index;
    var url = '#fragment-' + index;

    addTab(url, title, index);
    $('li[class=ui-state-default]').id(this)=1; // this line to add id

});

しかし、IDはタブに割り当てられていません..

JSフィドル

4

3 に答える 3

2
 $('li[class=ui-state-default]').id(this)=1

なるべき

 $('YOUR_NEW_TAB_SELECTOR').attr('id',1)

.attr メソッドを使用して、属性「id」を「1」に設定します。

于 2012-05-17T08:26:48.410 に答える
1

ひやデモ: ) http://jsfiddle.net/gP3YZ/7/

コード

$(document).ready(function() {
    $("#tabs").tabs({
        tabTemplate: "<li><a href='#{href}'>#{label}</a> <p title='close' id='removeTab' style='cursor:pointer;display:inline'>x</p></li>"
    });
});

$(function() {
    var index = 0;
    $("#addTab").live('click', function() {
        index++;
        var title = 'Tab.....  ' + index;
        var url = '#fragment-' + index;

        addTab(url, title, index);
        $('li.ui-state-default').attr("id","1");
        alert($('li.ui-state-default').attr("id"));

    });

    function addTab(url, title, index) {

        $('#tabs').tabs("add", url, title, [index]);
    }
    $('#removeTab').live('click', function() {
    selected = $('p[id=removeTab]').index(this); // this line to add id
      $('#tabs').tabs("remove", [selected]);

    });


     $('#appendText').live('click', function() {
        $('#tabs .ui-tabs-panel').each(function(index) {

             if(!($(this).hasClass('ui-tabs-hide'))){
                 //do the dew!
                 $(this).append("Bla Bla!!!");

             }
        });
    });


});



​
于 2012-05-17T08:32:43.337 に答える
1
$('li.ui-state-default:last').attr('id', 'some_' + index); // only Numeric id not allowed

デモ

注: $('li.ui-state-default').attr('id', 'some_' + index);すべての li の id has classui-state-defaultを変更しましたが、現在のコードは最後の id のみを変更します。

于 2012-05-17T08:28:16.697 に答える