javascript - 如何使用切换仅显示少数?

标签 javascript jquery html css

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

这很好用。但我想用一张 table 做同样的事情(切换)。正如您在这个工作 fiddle 中看到的那样,它一开始只显示了一些信息,但是当您单击该按钮时,它会显示更多信息。我怎样才能用 table 做同样的事情?

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

最佳答案

根据您的表格和评论,您可以使用以下 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

关于javascript - 如何使用切换仅显示少数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26018322/

相关文章:

单击文本时 JavaScript 不提交隐藏表单

javascript - 如何让 DIV 占据容器 div 的其余高度?

javascript - 为什么 Javascript Date.now() 返回错误的日期?

javascript - 将对象的边缘彼此对齐并防止重叠

javascript - 如何在 ng serve 上缩小 vendor.js

jquery - 在 jQuery 中反转这个

javascript - jQuery/javascript 中的全局四舍五入数学

javascript - 未显示 Google map 自动完成建议

HTML/CSS 图像分隔符/分隔符叠加/方法?

javascript - 如何隐藏单个选择的jquery搜索?