douquanhui5735 2015-08-14 10:31
浏览 166

显示动态添加的选项卡jquery选项卡

I'm creating a web-application in which user adds a tab and put his content. This tabs are dynamically created. I searched for it on google and tried for given solutions but none of them worked for me. So i posted a question here.

My code is:

<div id="tabs" class="htabs">
    <a href="#tab-general"><?php echo $tab_general; ?></a>
    <a onclick="addtab();" id="add-tab"><?php echo $add_tab; ?></a>
</div> 

And script is :-

<script type="text/javascript">

$('#tabs a').tabs(); 

var tab_count = '<?php echo $tab_count; ?>';

function addtab(){

    var html = '';

    $('#add-tab').before('<a href="#product-tab-'+tab_count+'" class="nearest">Tab '+tab_count+'</a>');

    html += '<div id="product-tab-'+tab_count+'" class="nthDiv">';
    html += '<table class="form">'
    html += '<tr>';

    html += '<td>';
    html += 'Name'+tab_count;       
    html += '</td>';

    html += '<td>';
    html += '<input type="text">';
    html += '</td>';

    html += '</tr>';
    html += '</table>';

    html += '</div>';

    $('#form').append(html);
    $('#tabs a').tabs("refresh");

    //$('#tabs a').tabs( "option", "selected", -1 ); tried but doesn't work

    //$('#tabs a').tabs("option", "active", -1); tried but doesn't work


    tab_count++;

}

</script>

My jquery version is jquery-1.7.1.min.js and jquery ui version is 1.8.

The problem is the new tab is not get active/selected. I have to manually click on that tab to make it active.

  • 写回答

1条回答

  • douhutongvm382381 2015-08-14 10:44
    关注

    Your markup for the tabs is wrong

    $('#tabs').tabs({
      beforeActivate: function(e, ui) {
        return ui.newTab.find('#add-tab').length == 0;
      }
    });
    
    var tab_count = 1;
    
    $('#add-tab').click(function() {
    
      var html = '';
    
      $('#add-tab').parent().before('<li><a href="#product-tab-' + tab_count + '" class="nearest">Tab ' + tab_count + '</a></li>');
    
      html += '<div id="product-tab-' + tab_count + '" class="nthDiv">';
      html += '<table class="form">'
      html += '<tr>';
    
      html += '<td>';
      html += 'Name' + tab_count;
      html += '</td>';
    
      html += '<td>';
      html += '<input type="text">';
      html += '</td>';
    
      html += '</tr>';
      html += '</table>';
    
      html += '</div>';
    
      $('#tabs').append(html);
      $('#tabs').tabs("refresh").tabs("option", "active", -2);
    
    
      tab_count++;
    
    })
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.js"></script>
    <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/redmond/jquery-ui.css"/>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.js"></script>
    
    <div id="tabs" class="htabs">
      <ul>
        <li><a href="#tab-general">Gen</a></li>
        <li><a href="#" id="add-tab">Add</a></li>
      </ul>
      <div id="tab-general">
        tab-general
      </div>
    
    </div>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?