如何用切换显示少数?

时间:2014-09-24 13:35:29

标签: javascript jquery html css

我有这个工作jsfiddle与标记和一些jquery。

这很好用。但我想用表做同样的事情(切换)。正如您在这个工作小提琴中看到的那样,它首先只显示了一些信息,但是当您单击按钮时,它会显示更多信息。我怎么能用桌子做同样的事情?

<section class="row-fluid">
    <!-- Price example section start -->
    <div class="innerAreaWithHeading innerAreaBottom padding span8 offset2 toggleContent">

        <h2 class="text-center h1">Header</h2>
        <p class="big">
            Text
        </p>

        <div class="nsRow innerAreaTop innerAreaBottom border-gray">
            <div class="nsSpan6">
                <div class="padding">
                    <h3 class="text-center">Header</h3>
                    <p class="price text-center text-pink xl">
                        Text
                    </p>
                </div>

            </div>
            <div class="nsSpan6">
                <div class="bg-gray padding priceBox">
                    <h3 class="text-center">Header</h3>
                    <p class="price text-center text-turquoise xl">
                        Text
                    </p>
                    <div class="priceTriangleDown"></div>
                </div>
            </div>
        </div>
        <!-- Toggle hide/show content -->
        <div class="nsRow innerAreaTop innerAreaBottom border-gray priceExaples" style="display: none;">
            <div class="nsSpan6">
                <div class="padding">
                    <h3 class="text-center">Header</h3>
                    <p class="price text-center text-pink xl">
                         Text
                    </p>
                </div>

            </div>
            <div class="nsSpan6">
                <div class="bg-gray padding priceBox">
                    <h3 class="text-center">Header</h3>
                    <p class="price text-center text-turquoise xl">
                        Text
                    </p>
                    <div class="priceTriangleDown"></div>
                </div>
            </div>
        </div>

        <div class="nsRow innerAreaTop innerAreaBottom border-gray priceExaples" style="display: none;">
            <div class="nsSpan6">
                <div class="padding">
                    <h3 class="text-center">Header</h3>
                    <p class="price text-center text-pink xl">
                        Text
                    </p>
                </div>

            </div>
            <div class="nsSpan6">
                <div class="bg-gray padding priceBox">
                    <h3 class="text-center">Header</h3>
                    <p class="price text-center text-turquoise xl">
                        Text
                    </p>
                    <div class="priceTriangleDown"></div>
                </div>
            </div>
        </div>
        <!-- End toggle hide/show content -->
        <div class="row-fluid">
            <button data-toggle=".priceExaples"
                data-togglevisibletext="See more examples"
                data-togglehiddentext="See less examples"
                class="btnRounded btnRounded-inverted">
                See less examples</button>
        </div>
    </div>
</section>

$('[data-toggle]').click(function () {
    var $this = $(this);
    var toggle = $this.attr("data-toggle");
    // Scope triggered area
    var $context = $(toggle);

    var attr1 = $(this).attr('data-toggleHiddenText');
    var attr2 = $(this).attr('data-toggleVisibleText');
    if (typeof attr1 !== typeof undefined && attr1 !== false &&
        typeof attr2 !== typeof undefined && attr2 !== false) {
        // Toggle text on buttons
        var txt = $context.is(':visible') ? attr1 : attr2;
        $this.text(txt);
    }

    // Show/hide more content
    $context.slideToggle('fast');

});

http://jsfiddle.net/dbtxs5rt/1/

1 个答案:

答案 0 :(得分:1)

根据你的表和评论,你可以使用以下jQuery实现你想要的东西:

var tbody = $('tbody'),
    rows = tbody.children('tr:gt(1)'),
    button = $('<button>Visa tabell</button>');

rows.hide().closest('table').after(button);

button.click(function () {    
    if (tbody.hasClass('shown')) {
        rows.fadeOut(function () {
            tbody.removeClass('shown');
            button.text('Visa tabell');
        });
    } else {
        rows.fadeIn(function () {
            tbody.addClass('shown');
            button.text('Dölj tabellen');
        });
    }          
});

Example