使用jquery/javascript切换语句和循环 [英] switch statement and loops using jquery/javascript

查看:50
本文介绍了使用jquery/javascript切换语句和循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以通过使用某种循环为我完成工作而在jquery/javascript中生成switch语句?例如,如果我有一个类似

Is there a way I can generate switch statements in jquery/javascript by using some sort of loop to do the work for me? For example, if I had a statement like:

switch ($("#play option:selected").text()) {
    case '1':
        $("#play_1").slideDown().find("input").addClass("someClass");
        break;
    case '2':
        $("#play_1").slideDown().find("input").addClass("someClass");
        $("#play_2").slideDown().find("input").addClass("someClass");
        break;
}

如果我的选择菜单中只有几个选项,那很好,但是我有99个选项,因此如果是'99',我必须显示99个新的div或它们可能是什么?

This is fine if I only have a few options in my select menu, but what I had 99 options and therefore by case '99' I had to display 99 new divs or whatever they might be?

推荐答案

for (var i = 1; i <= $("#play option:selected").text(); ++i) {
    $("#play_"+i).slideDown().find("input").addClass("someClass");
}

如果选择10,则该循环将找到从#play_1#play_10的十个元素并对其进行动画处理.

If you select 10 that loop will find the ten elements from #play_1 to #play_10 and animate them.

这篇关于使用jquery/javascript切换语句和循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆