帮助理解jQuery按钮启用/禁用代码 [英] Help understanding jQuery button enable/disable code

查看:132
本文介绍了帮助理解jQuery按钮启用/禁用代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我抓住了这个代码形式JCarousel,并试图理解下面的这些行。我是jQuery的新手,在JavaScript上不是很好,所以我不确定什么是jQuery,哪个是下面的JavaScript

I grabbed this code form JCarousel and just trying to understand these lines below. I'm new to jQuery and not that great at JavaScript so I am not sure what is jQuery and which is JavaScript below

this.buttonNext[n ? 'bind' : 'unbind'](this.options.buttonNextEvent, this.funcNext)[n ? 'removeClass' : 'addClass'](this.className('jcarousel-next-disabled')).attr('disabled', n ? false : true);
this.buttonPrev[p ? 'bind' : 'unbind'](this.options.buttonPrevEvent, this.funcPrev)[p ? 'removeClass' : 'addClass'](this.className('jcarousel-prev-disabled')).attr('disabled', p ? false : true);

它设置一些css来设置状态,并启用或禁用其中的按钮一旦我真正理解它,我想修改它。我只是无法弄清楚它正在做什么100%。

It's setting some of the css to set state and either enabling or disabling the button that is in a but I want to modify this once I really understand it. I just can't make out exactly what it's doing 100%.

试图理解诸如[n? 'bind':'unbind']而且这里也只是链接。这4行中有很多内容。

Trying to understand things such as [n ? 'bind' : 'unbind'] and just the chaining here also. There's a lot going on in those 4 lines.

代码来自这个插件: http://sorgalla.com/projects/jcarousel/

推荐答案

要理解的第一部分是符号解析。 Javacript支持点符号和括号表示法。

The first part to understand is symbol resolution. Javacript supports both dot-notation and bracket-notation.

考虑打开一个新窗口。

window.open()

这是点符号。你告诉翻译,开放可以在窗口找到。但还有另一种方法可以做到这一点

This is dot-notation in action. you're telling the interpreter that "open" can be found on "window". But there's another way to do this

window['open']()

同样的事情,但用括号表示法。我们不是直接使用符号名称,而是使用字符串文字。这意味着通过使用括号表示法进行符号解析,我们可以动态地进行,因为我们可以动态选择或构建字符串,这正是这个代码片段的作用。

Same thing, but with bracket notation. Instead of using the symbol name directly, we're using a string literal instead. What this means is that by using bracket-notation for symbol resolution, we can do it in a dynamic way, since we can choose or build strings on the fly, which is exactly what this snippet does.

this.buttonNext[n ? 'bind' : 'unbind'](...);

是否与

if ( n )
{
   this.buttonNext.bind(...);
} else {
   this.buttonNext.unbind(...);
}

如果你不认识?:语法,那就是条件运算符或条件表达式

If you don't recognize the ?: syntax, that's the conditional operator, or conditional expression

[expression] ? [valueIfTrue] : [valueIfFalse]

这在经常被错误地称为三元运算符事实上它只是一个三元运算符(一个有三个操作数的运算符)。这是因为在javascript(和大多数语言)中只有 三元运算符,因此描述通常过得很快。

This is extremely often erroneously called the "ternary operator", when in fact it just a ternary operator (an operator with three operands). The reason for this is because in javascript (and most languages) is the only ternary operator, so that description usually flies.

这有帮助吗? ?还有什么需要清理吗?

Does that help? is there anything else you need cleared up?

这篇关于帮助理解jQuery按钮启用/禁用代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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