这行JQuery代码是什么意思?

时间:2022-09-06 14:21:30

I came across this post on this site with a jFiddle showing a following menu for JQUery, well I saw this piece of syntax I can't figure out.

我在这个网站上看到了这篇文章,一个jFiddle显示JQUery的菜单,我看到了这个我无法理解的语法。

JFiddle: http://jsbin.com/oxajeq/3/edit?html,css,js,console,output

JFiddle:http://jsbin.com/oxajeq/3/edit?html、css js,控制台输出

Line of code I do NOT understand

代码行我不明白

$('#mini-logo')[logoSH](300);

I know the first part selects the element with id of mini-logo, but I have no idea what the rest of the syntax is! in the code, [logoSH] can become show or hide, while the () at the end means the duration. However, I can't find any example of anything using this syntax. I also googled for CSS3, JQUery, transitions, effects, animations, anything of what this might be and no luck. I find stuff that are methods, and others that are not methods but take parameters, but nothing like this code. I know that what ever is inside [] is not a method, but I can't figure out what they are. thanks in advance for any help.

我知道第一部分选择id为mini-logo的元素,但是我不知道后面的语法是什么!在代码中,[logger]可以变成show或hide,而结尾的()表示持续时间。但是,我找不到任何使用这种语法的例子。我还在谷歌上搜索了CSS3、JQUery、transition、effects、animation,以及任何可能出现的东西,但都没有运气。我发现了一些方法,有些不是方法,而是参数,但是没有像这样的代码。我知道[]里面的东西不是一种方法,但我不知道它们是什么。谢谢你的帮助。

1 个解决方案

#1


11  

This construct is based on the bracket notation to access properties. It allows here a dynamic selection of the method to apply (show or hide).

这个构造基于访问属性的括号表示法。它允许动态选择要应用的方法(显示或隐藏)。

logoSH is either "show" or "hide".

loomg要么是“show”,要么是“hide”。

Which means your line is either

也就是说你的线是任意的

$('#mini-logo')["show"](300); or $('#mini-logo')["hide"](300);

$(" # mini-logo”)(“秀”)(300);美元(“# mini-logo”)(“隐藏”)(300);

which you can also read as

你也可以读成

$('#mini-logo').show(300); or $('#mini-logo').hide(300);

$(' # mini-logo '),告诉(300);美元(# mini-logo)hide(300);

This is a common construct, that you may also find with a ternary operator:

这是一个常见的结构,你也可以用三元算符找到:

$('#mini-logo')[someBool ? "show" : "hide"](300);

Note: were there not the duration, you could have use the toggle function which takes a boolean as argument.

注意:如果没有持续时间,您可以使用toggle函数,它以布尔值作为参数。

#1


11  

This construct is based on the bracket notation to access properties. It allows here a dynamic selection of the method to apply (show or hide).

这个构造基于访问属性的括号表示法。它允许动态选择要应用的方法(显示或隐藏)。

logoSH is either "show" or "hide".

loomg要么是“show”,要么是“hide”。

Which means your line is either

也就是说你的线是任意的

$('#mini-logo')["show"](300); or $('#mini-logo')["hide"](300);

$(" # mini-logo”)(“秀”)(300);美元(“# mini-logo”)(“隐藏”)(300);

which you can also read as

你也可以读成

$('#mini-logo').show(300); or $('#mini-logo').hide(300);

$(' # mini-logo '),告诉(300);美元(# mini-logo)hide(300);

This is a common construct, that you may also find with a ternary operator:

这是一个常见的结构,你也可以用三元算符找到:

$('#mini-logo')[someBool ? "show" : "hide"](300);

Note: were there not the duration, you could have use the toggle function which takes a boolean as argument.

注意:如果没有持续时间,您可以使用toggle函数,它以布尔值作为参数。