简体   繁体   English

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

[英]Display dynamically added tab jquery tabs

I'm creating a web-application in which user adds a tab and put his content. 我正在创建一个Web应用程序,用户在其中添加标签并放置其内容。 This tabs are dynamically created. 该选项卡是动态创建的。 I searched for it on google and tried for given solutions but none of them worked for me. 我在Google上搜索了它,并尝试了给定的解决方案,但没有一个对我有用。 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 . 我的jquery版本是jquery-1.7.1.min.js和jquery ui版本是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. 我必须手动单击该选项卡才能将其激活。

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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM