Py学习  »  Jquery

jquery开关盒插件?

Mottie • 5 年前 • 549 次点击  

我知道switch case语句是JavaScript固有的,您不能更改它。我仍然在学习javascript和jquery,所以我可以通过,但我不知道足够的知识来写一些可能在jquery本身级别上的东西。所以,把这当作一个想法或者一个关于这个想法是否可行的问题。

这是我的想法,一个可以使用对象的switch case语句…可以这样使用:

switch( $(this) ){

 case .hasClass('foo'):
  // do something
  break;

 case .filter('.bar').attr('id') == 'foo':
  // do something else
  break;

}

编辑:即使是这样的事情也会很好(可能是一个更合理的想法)。

switch ($(this).hasClass()) {

 case 'foo':
  alert('found a foo!');
  break;

 case 'bar':
  alert('found a bar!');
  break;

}
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/30860
 
549 次点击  
文章 [ 5 ]  |  最新文章 5 年前
Christian Haller
Reply   •   1 楼
Christian Haller    12 年前
    (function($) {
    $.fn.switchClasses = function(switches) {
        return this.each(function() {
            var that = this;
            $.each(this.attr("class").split(' '), function(i, classname) {
                var func = switches[classname];
                if(func){
                    func.apply(that, classname);
                }
            });
        });
    };
})(jQuery);

“class”是保留名称, 我加了一些缺少的括号

Bill Criswell
Reply   •   2 楼
Bill Criswell    14 年前

你可以打开 element.attr('class') 如果你只处理一个类名。

如果你和不止一个人打交道 element.attr('class').split(/\s+/) 并检查该数组中的类名称。

我也认为你可能想看看 .is() . 你可以做像 if( element.is('a.foo') ) console.log( 'This is a link with a 'foo' class.' );

不过,您可能想改变您的方法。我不认为这是做你想做的事情的最有效的方法。

Andrew Hare
Reply   •   3 楼
Andrew Hare    14 年前

正常人怎么了 if/else ?

inst = $(this);

if (inst.hasClass('foo')) {
    // do something
} else if (inst.filter('.bar').attr('id') == 'foo') {
    // do something else
}
Joe K
Reply   •   4 楼
Joe K    12 年前

首先,Switter语句只在int上工作。我不知道JavaScript为什么会从C/C++中保留这个限制,但是它在那里。

使用嵌套的if-then-else块(而不是开关)可能会导致代码高度不可读,如果您处理的选项不止一小部分。

但是,就像C和C++一样,也有一些解决方法,这一点涉及到使用GOTO这样的“断裂”,这并不总是邪恶的。这是一种情况(大量嵌套,如果是else的话),goto将使代码更加高效和可读。在C/C++中,你将使用Goto来实现这一点,标签位于IF系列的末端(现在是交换机支架的末端),跳过跳转到交换机上下文。

switch (1) { //yes, that is a hardcoded 1, we only want the switch block for the break keyword
    if (yourString == "Case 1") {
        do_stuff_1();
        break; // well, we're done, let's get out of here.
    }

//implicit else - if you matched the first one, you'd be out of here

    if (yourString == "Case 2") {
        do_stuff_2();
        break; // well, we're done, let's get out of here.
    }

    // etc.....

    default:
        do_error_condition();
} //end of switch
kbrinley
Reply   •   5 楼
kbrinley    11 年前

通常情况下,开关不是解决问题的最佳方法,但是使用jquery,您可以创建一个类似switch语句的插件:

(function($) {
    $.fn.switchClasses = function(switches) {
        return this.each(function() {
            var that = this;
            $.each(this.attr("class").split(' '), function(i, class) {
                var func = switches[class];
                if(func)
                    func.apply(that, class);
            });
        });
    };
})(jQuery);

您可以这样使用插件:

$(this).switchClasses(
    {
        "class1":
        function(class) {
            // Do something for this class, 
            // the "this" variable is the jquery object that matched the class
        },

        "class2":
        function(class) {

        }
    }
);