JQuery ui选项卡没有正确更新

时间:2014-01-31 10:09:42

标签: javascript jquery jquery-ui jquery-ui-tabs

由于jquery-ui选项卡使用id而不是类标签,因此我创建了一个函数来为我的所有类生成id。现在我想添加新内容并将其应用于新内容,但添加新内容后,所有其他标签都停止工作!

这是html内容:

<div class="accordion">
    <h3>Product x</h3>
    <div class="tabs">
        <ul>
            <li><a href="" class="tabpanellink">General product info</a></li>
            <li><a href="" class="tabpanellink">End user info</a></li>
        </ul>
        <div class="tabpanel">
            general info for product x.
        </div>
        <div class="tabpanel">
            end user info product x.
        </div>
    </div>
</div>
<div class="accordion">
    <h3>Product y</h3>
    <div class="tabs">
        <ul>
            <li><a href="" class="tabpanellink">General info</a></li>
            <li><a href="" class="tabpanellink">End user info</a></li>
        </ul>
        <div class="tabpanel">
            general info for product y.
        </div>
        <div class="tabpanel">
            end user info product y.
        </div>
    </div>
</div>

这就是我试图用它做的事情:

function fixtabs(){
    $(".tabpanellink").each(
        function(uniqueindex){
            $(this).attr('href', '#tab-' + uniqueindex);
        }
    );
    $(".tabpanel").each(
        function(uniqueindex){
            $(this).attr('id', 'tab-' + uniqueindex);
        }
    );

    $(".accordion").accordion({
        collapsible: true,
        active: false
    });
}
function updateDocument(){
    fixtabs();
    $(".tabs").tabs();
}

function addProduct(){
    //Add another product(this is a server response in a real case scenario):
    var newProduct = '';
    newProduct += '<div class="accordion">';
    newProduct +=   '<h3>Product z</h3>';
    newProduct +=   '<div class="tabs">';
    newProduct +=       '<ul>';
    newProduct +=           '<li><a href="" class="tabpanellink">General product info</a></li>';
    newProduct +=           '<li><a href="" class="tabpanellink">End user info</a></li>';
    newProduct +=       '</ul>';
    newProduct +=       '<div class="tabpanel">';
    newProduct +=           'general info for new product.';
    newProduct +=       '</div>';
    newProduct +=       '<div class="tabpanel">';
    newProduct +=           'end user info product x.';
    newProduct +=       '</div>';
    newProduct +=   '</div>';
    newProduct += '</div>';
    $(".accordion").first().before(newProduct);
    updateDocument();
}
updateDocument();
addProduct();

这是jsfiddle

如何在不破坏现有标签的情况下对新内容应用标签效果?

更新以回复答案:我无法移除updateProduct();!我必须先调用updateDocument才能初始化所有内容! addProduct仅在事后调用(即当用户点击添加产品时!)

2 个答案:

答案 0 :(得分:2)

现在尝试使用添加产品选项

使其正常工作

**

这是fiddle

**

将另一个Div添加为父Div

<div id="accor">    

<div class="accordion">
        <h3>Product x</h3>
        <div class="tabs">
            <ul>
                <!-- .tabs-0 & .tabs-1 don't work, since they are not a valid url -->
                <li><a href="" class="tabpanellink">General product info</a></li>
                <li><a href="" class="tabpanellink">End user info</a></li>
            </ul>
            <div class="tabpanel">
                general info for product x.
            </div>
            <div class="tabpanel">
                end user info product x.
            </div>
        </div>
    </div>
    <div class="accordion">
        <h3>Product y</h3>
        <div class="tabs">
            <ul>
                <!-- .tabs-0 & .tabs-1 don't work, since they are not a valid url -->
                <li><a href="" class="tabpanellink">General info</a></li>
                <li><a href="" class="tabpanellink">End user info</a></li>
            </ul>
            <div class="tabpanel">
                general info for product y.
            </div>
            <div class="tabpanel">
                end user info product y.
            </div>
        </div>
    </div>
</div>
<button id="AddProduct"> add product</button >

在你的Jquery代码中会是这样的 -

function fixtabs(){
    $(".tabpanellink").each(
        function(uniqueindex){
            $(this).attr('href', '#tab-' + uniqueindex);
            uniqueindex++;
        }
    );
    uniqueindex = 0;
    $(".tabpanel").each(
        function(uniqueindex){
            $(this).attr('id', 'tab-' + uniqueindex);
            uniqueindex++;
        }
    );

    $(".accordion").accordion({
        collapsible: true,
        active: false
    });
}
function updateDocument(){
    fixtabs();
    $(".tabs").tabs();
}

function addProduct(){
    //Add another product:
    var newProduct = '';
    newProduct += '<div class="accordion">';
    newProduct +=   '<h3>Product z</h3>';
    newProduct +=   '<div class="tabs">';
    newProduct +=       '<ul>';
    newProduct +=           '<li><a href="" class="tabpanellink">General product info</a></li>';
    newProduct +=           '<li><a href="" class="tabpanellink">End user info</a></li>';
    newProduct +=       '</ul>';
    newProduct +=       '<div class="tabpanel">';
    newProduct +=           'general info for new product.';
    newProduct +=       '</div>';
    newProduct +=       '<div class="tabpanel">';
    newProduct +=           'end user info product x.';
    newProduct +=       '</div>';
    newProduct +=   '</div>';
    newProduct += '</div>';
    $("#accor").first().append(newProduct);
    updateDocument();
   //$(".tabs").tabs();
    //fixtabs();
}
//addProduct();
updateDocument();

$('#AddProduct').click(function(){
addProduct();
//$(".tabs").tabs();
//updateDocument();
});

答案 1 :(得分:0)

现在工作正常 -

只需在addProduct();

之前致电updateDocument();功能

小提琴here

function fixtabs(){
$(".tabpanellink").each(
    function(uniqueindex){
        $(this).attr('href', '#tab-' + uniqueindex);
        uniqueindex++;
    }
);
uniqueindex = 0;
$(".tabpanel").each(
    function(uniqueindex){
        $(this).attr('id', 'tab-' + uniqueindex);
        uniqueindex++;
    }
);

$(".accordion").accordion({
    collapsible: true,
    active: false
});
}

function updateDocument(){
    fixtabs();
    $(".tabs").tabs();
}

function addProduct(){
    //Add another product:
    var newProduct = '';
    newProduct += '<div class="accordion">';
    newProduct +=   '<h3>Product z</h3>';
    newProduct +=   '<div class="tabs">';
    newProduct +=       '<ul>';
    newProduct +=           '<li><a href="" class="tabpanellink">General product info</a></li>';
    newProduct +=           '<li><a href="" class="tabpanellink">End user info</a></li>';
    newProduct +=       '</ul>';
    newProduct +=       '<div class="tabpanel">';
    newProduct +=           'general info for new product.';
    newProduct +=       '</div>';
    newProduct +=       '<div class="tabpanel">';
    newProduct +=           'end user info product x.';
    newProduct +=       '</div>';
    newProduct +=   '</div>';
    newProduct += '</div>';
    $(".accordion").first().before(newProduct);
    updateDocument();
}
addProduct();
updateDocument();