English 中文(简体)
jqquery 重声明$( 此)
原标题:jquery re-declare $(this)

$. kick () 函数之后无法重新声明 $. (this) 吗? 因为这些功能似乎都行不通 :

$(this) = $(this).find( span );
var $(this) = $(this).find( span );
最佳回答

foo 是一个合法的识别符 时,您只能声明 var foo

$( this) 调用 函数的结果, 该函数命名为 $ , 带有参数 this 因此在声明中不合法 。

您也不应该覆盖 < code> this -- -- 这将在未来引起很大的头部抓伤!!

如果您想要存储 jQuery 版本 this 的本地变量, 普通公约为 :

var $this = $(this);
var $span = $this.find( span );

$ 前缀允许您记住变量是jQuery 对象,而不是普通的 DOM 元素时,该变量(完全合法,但有时被扭曲) $ 前缀允许您记住该变量是一个 jQuery 对象。

该公约还允许你发现这样做的浪费(但常见)错误:

var jqobj = $(myobj)

myobj 已经 a jQuery 对象时 。

问题回答

$( This) 表达式不是一个变量声明,而是一个表达式。 如果您想要重新声明某个您需要将其存储在变量中的东西, 则该表达式是一个表达式。

var saved = $(this);
saved = $(this).find( span );

最终版本之所以有效,是因为您正在将它指派给一个实际的标识符。

$( some ).on( click , function() {
  var refrence = $(this);
  var span = refrence.find( span )
  // to override the $this you can use
  refrence = refrence.find( span );
});




相关问题
"this" keyword in IE

So, I using this snippet <tr onclick="this.toggleClassName( selected )"> on a web page. (a Prototype.js function) It works great. All I do is click on a table row, and it appears to get ...

Accessing an outer class from inside a listener?

I have a listener inside Class A, and I want to pass Class A to my Class B inside the listener. Normally I d just use this, but then I d get the event that triggered the listener.

this, current context-when should I use in jQuery?

I am not very sure with the use of "this" [current context] in jquery.What I know is- it prevents the dom from searching all the elements, it just work on that current element, which improve ...

Why is this not updating to refer to a new object?

I m writing an online game which allows a user to progress from one puzzle to the next, and if the user makes mistakes, each puzzle has a start again button to allow the user to start just that puzzle ...

Should the RequireThis check in Checkstyle be enabled?

One of the built-in Checkstyle checks is RequireThis, which will go off whenever you don t prepend this. to local field or method invocations. For example, public final class ExampleClass { ...

热门标签