我使用以下变量选择了一个控件
var txt = $("#text1");
Run Code Online (Sandbox Code Playgroud)
现在,当我必须处理文本框上的事件时,我是否必须将其引用为$(txt)或txt
$(txt).keydown(function() {})
Run Code Online (Sandbox Code Playgroud)
要么
txt.keydown(function(){})
Run Code Online (Sandbox Code Playgroud)
有什么好处.请解释它以变量txt作为上下文.
最好的方法是声明变量,以便知道它们是什么.基本上,我所说的是应用匈牙利语的一些应用程序,并用$前缀jQuery变量
var $text1 = $("#text1"); // this is a jQuery object
var text1 = $text1[0]; // this is not
Run Code Online (Sandbox Code Playgroud)