添加类不起作用

时间:2011-05-21 06:53:56

标签: jquery jquery-ui

您好我正在jquery中动态创建一个表。我在这里做的还是添加“draggable ui-widget”类,但它不影响/在我的桌面上工作。

   var tab_title = $tab_title_input.val() || 'Tab '+tab_counter;
        //alert(tab_title);
        $tabs.tabs('add', '#tabs-'+tab_counter, tab_title);                 

        var newTableDiv = $("<div />",{id: 'dialog'+tab_counter});          
        newTableDiv.appendTo("body");           
        alert("div appended to body"+" "+'dialog'+tab_counter);
        //var newTable = $('<table id="myTable'+tab_counter+'" class="draggable" ></table>');
********************Class which i am adding in table but not working***********
        var newTable = $("<table />",{"class": "draggable ui-widget",id: 'myTable'+tab_counter,width: '100%',border: '0',cellspacing: '1px',cellpadding: '2px'});                       
        newTable.appendTo('#dialog'+tab_counter);           
        alert('myTable'+tab_counter);   
        newTable.append('<thead class="ui-widget-header"><tr></tr></thead>');
        alert("Header Created");
        var thead = $('thead tr', newTable);
        thead.append('<th><strong>Symbol</strong></th>');
        thead.append('<th><strong>Price</strong></th>');
        thead.append('<th><strong>Volume</strong></th>');
        thead.append('<th><strong>Buy</strong></th>');
        thead.append('<th><strong>Sell</strong></th>');
        alert("Header Created");            

我到底做错了什么。我对jquery很新,并且仍在努力。

3 个答案:

答案 0 :(得分:0)

实际上你可以在jquery中轻松创建一个表:

var table = "<table class='draggable ui-widget'>" +
            "<thead><tr><th></th></tr></thead>"+
            "<tbody>"+
            "<tr><td></td></tr>"+
            "<tr><td></td></tr>"+
            "</tbody></table>";

然后通过(#divid).append(table);将其附加到您的div 这应该有用,也很容易。

答案 1 :(得分:0)

添加课程是不够的。 AFAIK你应该明确指出你的元素是可拖动的。例如,为表格元素<table id='draggable'...添加ID,然后将其设为可拖动$('#draggable').draggable();

顺便说一句dom操作是非常昂贵的。所以在性能成本方面更好首先创建整个表格html,如@tittletipz指出,然后将其附加到你的div。

答案 2 :(得分:0)

通常情况下无法访问动态添加的课程

在你的例子中,如果我必须在click事件上触发一个函数,我就不能使用

$('.draggable').click(function{
});

我应该使用

 $('.draggable').live('click', function{
    });

因为绑定到事件只能像这样发生。但是你还没有提到你想如何使用这个类