سؤال

Since the jquery-ui tab works with ids instead of class tags, I created a function to generate ids for all my classes. Now I want to add new content and apply the same to that new content, but after adding new content all other tabs stop working!

This is the html content:

<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>

And this is what I'm trying to do with it:

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();

Here is the jsfiddle.

How to apply the tab effect on the new content without corrupting the existing tabs?

Update in response to answers: I can't remove updateProduct();! I have to call updateDocument first so everything is initialized! addProduct is only called afterwards (i.e. when user clicks add product!)

هل كانت مفيدة؟

المحلول

Now try this its working fine with your add product option

**

Here is fiddle

**

Add another Div as parent 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 >

And at your Jquery code will be like this -

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();
});

نصائح أخرى

Its working fine now -

Just call your addProduct(); function before updateDocument();

Fiddle 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();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top