当我单击页面中的链接时,我将打开一个 jquery 对话框(如果尚未打开)并在其中添加一个 jquery ui 选项卡。遵循 here 的示例
但是当我关闭 jquery 对话框并重新打开它时,之前打开的选项卡会显示出来。当我关闭并重新打开对话框时,我希望 jquery 对话框内没有选项卡,以便我可以在对话框内添加一个新选项卡。
这是我的 jquery 文档中的代码:
<script type="text/javascript">
var tab_counter = 1;
$(document).ready(function() {
var $tabs_example_1 = $('#example_1').tabs();
$('#addUiTab').click(function(){
var label = 'Tab'+tab_counter,
content = '<span id="tab">This is a sample tab content</span>';
$('body').append('<div id="tab_'+tab_counter+'">'+content+'</div>');
$tabs_example_1.tabs('add','#tab_'+tab_counter,label);
tab_counter++;
return false;
});
});
</script>
这是我的函数,当我单击页面中的链接时,它会打开一个对话框(如果尚未打开)并在其中添加一个选项卡:
function open_dialog()
{
if(($("#jdialog_box_content").dialog( "isOpen" ) === true) == false)
{
$('#jdialog_box_content').dialog({
open: function(event, ui) { },
close: function(event, ui) { },
autoResize: false,
width: 460,
height: 230,
closeOnEscape: false,
title: 'Dialog1'
});
}
$('#addUiTab').trigger("click");
}
最后,这是用于 jquery 对话框内的 jquery 选项卡的 html:
<div id="jdialog_box_content" style="display:none;">
<div id="example_1" class="tabs">
<ul>
<li><a href="#tabs-1-2">Tab 1</a></li>
<li><a href="#tabs-2-2">This is tab 2</a></li>
<li><a href="#tabs-3-2">This is tab number 3</a></li>
</ul>
<div id="tabs-1-2">Tab 1<br>Lorem ipsum dolor sit amet</div>
<div id="tabs-2-2">This is tab 2<br>Lorem ipsum dolor sit amet</div>
<div id="tabs-3-2">This is tab 3<br>Lorem ipsum dolor sit amet</div>
</div>
</div>
我只是想知道当重新打开 jquery 对话框时如何删除它中的现有选项卡。
注意:我正在关注 example at this site用于 jquery ui 选项卡。
最佳答案
您需要close: function(event, ui) { $("#jdialog_box_content").html(""); }
在你的对话框声明中。
如果您不需要空对话框并且不需要选项卡,您可以这样做:
$('#jdialog_box_content').dialog({
open: function(event, ui) { },
close: function(event, ui) {
var $tabs = $('#example_1');
var l = $tabs.tabs('length');
while(l)
{
$tabs.tabs('remove', l-1);
l = $tabs.tabs('length');
}
},
autoResize: false,
width: 460,
height: 230,
closeOnEscape: false,
title: 'Dialog1'
});
关于jquery - 关闭时清除 jquery 对话框内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10714869/