简体   繁体   English

在jQuery中实现以下动态

[英]Making the following dynamic in jQuery

I have a script that I have created in jQuery, which works how I would like it to, however I just know there will be a simpler, more dynamic way of doing the following. 我有一个用jQuery创建的脚本,该脚本可以按我希望的方式工作,但是我只知道会有一种更简单,更动态的方法来执行以下操作。

I was wondering whether someone could help me out? 我想知道是否有人可以帮助我?

Ok, so my HTML structure is: 好的,所以我的HTML结构是:

<div class="one_category category_list">
    <p>Option for 1 go here</p>
</div>

<div class="two_category category_list">
    <p>Option for 2 go here</p>
</div>

Please note, the above is duplicated ten times, with just the class name changing. 请注意,以上内容重复了十次,只是更改了类名。 I can easily change the markup if it would be easier to include numbers. 如果更容易包含数字,则可以轻松更改标记。

Then my jQuery, which can be condensed and made dynamic: 然后是我的jQuery,可以将其压缩并使其动态化:

$(document).ready(function() {

    // Grid Layout - Number of Categories //

        var number_of_categories = $('.number_of_categories_list');
        if ( $(number_of_categories).val() == '1' ) {
            $('.one_category').show();
        } else if ( $(number_of_categories).val() == '2' ) {
            $('.one_category').show();
            $('.two_category').show();
        } else if ( $(number_of_categories).val() == '3' ) {
            $('.one_category').show();
            $('.two_category').show();
            $('.three_category').show();
        } else if ( $(number_of_categories).val() == '4' ) {
            $('.one_category').show();
            $('.two_category').show();
            $('.three_category').show();
            $('.four_category').show();
        } else if ( $(number_of_categories).val() == '5' ) {
            $('.one_category').show();
            $('.two_category').show();
            $('.three_category').show();
            $('.four_category').show();
            $('.five_category').show();
        } else if ( $(number_of_categories).val() == '6' ) {
            $('.one_category').show();
            $('.two_category').show();
            $('.three_category').show();
            $('.four_category').show();
            $('.five_category').show();
            $('.six_category').show();
        } else if ( $(number_of_categories).val() == '7' ) {
            $('.one_category').show();
            $('.two_category').show();
            $('.three_category').show();
            $('.four_category').show();
            $('.five_category').show();
            $('.six_category').show();
            $('.seven_category').show();
        } else if ( $(number_of_categories).val() == '8' ) {
            $('.one_category').show();
            $('.two_category').show();
            $('.three_category').show();
            $('.four_category').show();
            $('.five_category').show();
            $('.six_category').show();
            $('.seven_category').show();
            $('.eight_category').show();
        } else if ( $(number_of_categories).val() == '9' ) {
            $('.one_category').show();
            $('.two_category').show();
            $('.three_category').show();
            $('.four_category').show();
            $('.five_category').show();
            $('.six_category').show();
            $('.seven_category').show();
            $('.eight_category').show();
            $('.nine_category').show();
        } else if ( $(number_of_categories).val() == '10' ) {
            $('.one_category').show();
            $('.two_category').show();
            $('.three_category').show();
            $('.four_category').show();
            $('.five_category').show();
            $('.six_category').show();
            $('.seven_category').show();
            $('.eight_category').show();
            $('.nine_category').show();
            $('.ten_category').show();
        }


    $('.number_of_categories_list').change(function(){
        if ($(this).val() == '1') { 
            $('.one_category').slideDown();
        } else if ($(this).val() == '2') { 
            $('.one_category').slideDown();
            $('.two_category').slideDown();
        } else if ($(this).val() == '3') { 
            $('.one_category').slideDown();
            $('.two_category').slideDown();
            $('.three_category').slideDown();
        } else if ($(this).val() == '4') { 
            $('.one_category').slideDown();
            $('.two_category').slideDown();
            $('.three_category').slideDown();
            $('.four_category').slideDown();
        } else if ($(this).val() == '5') { 
            $('.one_category').slideDown();
            $('.two_category').slideDown();
            $('.three_category').slideDown();
            $('.four_category').slideDown();
            $('.five_category').slideDown();
        } else if ($(this).val() == '6') { 
            $('.one_category').slideDown();
            $('.two_category').slideDown();
            $('.three_category').slideDown();
            $('.four_category').slideDown();
            $('.five_category').slideDown();
            $('.six_category').slideDown();
        } else if ($(this).val() == '7') { 
            $('.one_category').slideDown();
            $('.two_category').slideDown();
            $('.three_category').slideDown();
            $('.four_category').slideDown();
            $('.five_category').slideDown();
            $('.six_category').slideDown();
            $('.seven_category').slideDown();
        } else if ($(this).val() == '8') {
            $('.one_category').slideDown();
            $('.two_category').slideDown();
            $('.three_category').slideDown();
            $('.four_category').slideDown();
            $('.five_category').slideDown();
            $('.six_category').slideDown();
            $('.seven_category').slideDown(); 
            $('.eight_category').slideDown();
        } else if ($(this).val() == '9') { 
            $('.one_category').slideDown();
            $('.two_category').slideDown();
            $('.three_category').slideDown();
            $('.four_category').slideDown();
            $('.five_category').slideDown();
            $('.six_category').slideDown();
            $('.seven_category').slideDown(); 
            $('.eight_category').slideDown();
            $('.nine_category').slideDown();
        } else if ($(this).val() == '10') { 
            $('.one_category').slideDown();
            $('.two_category').slideDown();
            $('.three_category').slideDown();
            $('.four_category').slideDown();
            $('.five_category').slideDown();
            $('.six_category').slideDown();
            $('.seven_category').slideDown(); 
            $('.eight_category').slideDown();
            $('.nine_category').slideDown();
            $('.ten_category').slideDown();
        } else { }
    });

});

Any ideas on how I can condense the jQuery elements? 关于如何精简jQuery元素的任何想法?

Thanks for your help as always. 与往常一样感谢您的帮助。

Hardly any code is needed. 几乎不需要任何代码。 Just use your common class and slice the selection based on the selected value: 只需使用您的通用类,然后根据所选值对所选内容进行切片:

http://jsfiddle.net/TrueBlueAussie/4vxeuvhm/ http://jsfiddle.net/TrueBlueAussie/4vxeuvhm/

$('.number_of_categories_list').change(function(){
    var count = $(this).val();
    var $lists = $('.category_list');    // All the lists
    var $show = $lists.slice(0,count);   // Just the ones we want to show
    var $hide = $lists.not($show);       // Everything that we do not want to show
    $show.slideDown();
    $hide.slideUp();
});
// trigger initial update
$('.number_of_categories_list').trigger("change");

I have intentionally left it unshortened further as this is intended as a readable example. 我故意将其进一步简化,因为这旨在作为可读的示例。

If you take it too far it becomes shorter but almost unreadable: 如果走得太远,它会变短但几乎不可读:

eg http://jsfiddle.net/TrueBlueAussie/4vxeuvhm/3/ 例如http://jsfiddle.net/TrueBlueAussie/4vxeuvhm/3/

var $lists = $('.category_list');
$lists.slice($('.number_of_categories_list').change(function () {
    var $show = $lists.slice(0, $(this).val()).slideDown();
    $lists.not($show).slideUp();
}).val()).hide();

Most likely it should be something like this: 最有可能应该是这样的:

// Grid Layout - Number of Categories //
var number_of_categories = $('.number_of_categories_list');
var categories_list = $('.categories_list');
categories_list.slice(0, number_of_categories.val()).show();

number_of_categories_list.change(function(){
    categories_list.slice(0, this.val()).slideDown();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM