0

如何让 JQuery UI 手风琴在动态添加的选项卡中工作?我希望它每次创建一个新选项卡时,都会在其中创建一个新的手风琴对象。这是一个使用 Jquery UI 站点的基本选项卡示例的代码示例,稍作修改以包含一个带有 2 个部分的手风琴。

截至目前,标签工作正常,但在新标签内,手风琴内容显示为常规 HTML,而不是手风琴 jquery 对象。

提前致谢。

<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Untitled 1</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link type="text/css" href="jquery-ui/css/smoothness/jquery-ui-1.8.22.custom.css" rel="stylesheet" />
<style>
#dialog label, #dialog input { display:block; }
#dialog label { margin-top: 0.5em; }
#dialog input, #dialog textarea { width: 95%; }
#tabs { margin-top: 1em; }
#tabs li .ui-icon-close { float: left; margin: 0.4em 0.2em 0 0; cursor: pointer; }
#add_tab { cursor: pointer; }
</style>
<script>
$(document).ready(function(){
   $( "#accordion" ).accordion();

   var tabTitle = $( "#tab_title" ),
        tabContent = $( "#tab_content" ),
        tabTemplate = "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close'>Remove Tab</span></li>",
        tabCounter = 2;

    var tabs = $( "#tabs" ).tabs();

    // modal dialog init: custom buttons and a "close" callback reseting the form inside
    var dialog = $( "#dialog" ).dialog({
        autoOpen: false,
        modal: true,
        buttons: {
            Add: function() {
                addTab();
                $( this ).dialog( "close" );
            },
            Cancel: function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
            form[ 0 ].reset();
        }
    });

    // addTab form: calls addTab function on submit and closes the dialog
    var form = dialog.find( "form" ).submit(function( event ) {
        addTab();
        dialog.dialog( "close" );
        event.preventDefault();
    });

    // actual addTab function: adds new tab using the input from the form above
    function addTab() {
        var label = tabTitle.val() || "Tab " + tabCounter,
            id = "tabs-" + tabCounter,
            li = $( tabTemplate.replace( /#\{href\}/g, "#" + id ).replace( /#\{label\}/g, label ) ),
            tabContentHtml = tabContent.val() || "Tab " + tabCounter + " content.";

        tabs.find( ".ui-tabs-nav" ).append( li );
        tabs.append( "<div id='" + id + "'><div id='accordion'><h3>header1</h3><div><p>PARAGRAPH1</p></div><h3>header2</h3><div><p>PARA2</p></div></div></div>" );
        tabs.tabs( "refresh" );
        tabCounter++;
    }

    // addTab button: just opens the dialog
    $( "#add_tab" )
        .button()
        .click(function() {
            dialog.dialog( "open" );
        });

    // close icon: removing the tab on click
    $( "#tabs span.ui-icon-close" ).live( "click", function() {
        var panelId = $( this ).closest( "li" ).remove().attr( "aria-controls" );
        $( "#" + panelId ).remove();
        tabs.tabs( "refresh" );
    });
});
</script>
</head>
<body>
<div id="dialog" title="Tab data">
    <form>
        <fieldset class="ui-helper-reset">
            <label for="tab_title">Title</label>
            <input type="text" name="tab_title" id="tab_title" value="" class="ui-widget-content ui-corner-all" />
            <label for="tab_content">Content</label>
            <textarea name="tab_content" id="tab_content" class="ui-widget-content ui-corner-all"></textarea>
        </fieldset>
    </form>
</div>
<button id="add_tab">Add Tab</button>
<div id="tabs">
    <ul>
        <li><a href="#tabs-1">Nunc tincidunt</a> <span class="ui-icon ui-icon-close">Remove Tab</span></li>
    </ul>
    <div id="tabs-1">
        <p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollicitudin mi sit amet mauris.</p>
    </div>
</div>
</body>
</html>
4

2 回答 2

2

将新选项卡附加到 DOM 后,您必须再次在新创建的元素上调用 .accordion() 。

于 2013-01-07T07:29:41.393 回答
0

只需替换这一行 -

tabs.append( "<div id='" + id + "'><div id='accordion'>"+tabContentHtml+"</div>" );

见这里 - http://jsfiddle.net/im4aLL/nLqmN/

于 2013-01-07T07:33:42.660 回答