我是谁-JQuery,Plugin,$this,$(this),this,访问变量
我在识别运行函数的对象时遇到问题我是谁-JQuery,Plugin,$this,$(this),this,访问变量,jquery,plugins,this,Jquery,Plugins,This,我在识别运行函数的对象时遇到问题 <div id="test1">lorem</div> <div id="test2">ipsum</div> <script type="text/javascript"> $(document).ready(function() { $('#test1').plugin(); //alert: I am test1 $('#test2').plugin();
<div id="test1">lorem</div>
<div id="test2">ipsum</div>
<script type="text/javascript">
$(document).ready(function() {
$('#test1').plugin(); //alert: I am test1
$('#test2').plugin(); //alert: I am test2
$('#test1').plugin.fun(); //alert: I am undefined and I am undefined
$('#test2').plugin.fun(); //alert: I am undefined and I am undefined
});
(function($) {
$.fn.plugin = function() {
$this = $(this);
alert('I am '+$(this).attr('id'));//<-- it works
}
$.fn.plugin.fun = function() {
alert('I am '+$(this).attr('id')); //<-- doesn't work!
alert('and I am '+$this.attr('id')); //<-- also doesn't work!
}
})(jQuery);
</script>
lorem
乱数假文
$(文档).ready(函数(){
$('#test1').plugin();//警告:我是test1
$('#test2').plugin();//警告:我是test2
$('#test1').plugin.fun();//警告:我未定义,我未定义
$('#test2').plugin.fun();//警告:我未定义,我未定义
});
(函数($){
$.fn.plugin=函数(){
$this=$(this);
alert('I am'+$(this.attr('id'));//要了解$('#test1').plugin.fun()
是如何在JavaScript函数中设置的,我们先从理论开始,然后再回到插件
在JavaScript中,此
完全由函数的调用方式定义。最常见的方法是将其设置为从对象属性调用函数的副产品:
var foo = {
msg: "I'm foo!",
bar: function() {
alert(this.msg);
}
};
foo.bar(); // alerts "I'm foo!"
该行foo.bar();
有三个不同的功能:
它从foo
对象检索bar
属性
它调用属性引用的函数
它是这样做的#2,this
指的是foo
(更多信息请点击此处。)
所以在电话里
$('#test1').plugin.fun();
…fun
中的将是插件
,因为我们通过插件
上的属性调用fun
您可以考虑使用jQuery UI所使用的机械结构,它有插件的“方法”,可以通过一个main函数来访问,其中的名称是字符串。例如,在jQuery UI >代码>可拖动< /代码>实例中,您可以调用这样的方法:
$('#test1').draggable('disable');
$('#test1').plugin('fun');
有关更多示例,请参见他们的,但基本上您的呼叫如下所示:
$('#test1').draggable('disable');
$('#test1').plugin('fun');
有关函数调用的详细信息:
调用函数时,还有另一种设置此
的方法:通过函数实例上的调用
和应用
函数:
var f = function() {
alert(this.msg);
};
var foo = {
msg: "I'm foo!"
};
f.call(foo); // alerts "I'm foo!"
f.apply(foo); // alerts "I'm foo!" too
call
和apply
调用函数,将this
值设置为传递给它们的第一个参数。call
和apply
之间的区别在于如何将参数传递给目标函数。call
只需向call
函数添加更多参数;使用apply
,您可以将一组参数作为apply
的第二个参数。示例:
function f(arg1, arg2) {
alert(this.msg + " (" + arg1 + ", " + arg2 + ")");
}
var foo = {
msg: "I'm foo!";
};
f.call(foo, "one", "two"); // Alerts "I'm foo! (one, two)"
// ^-- "one" and "two" are discrete arguments
f.apply(foo, ["one", "two"]); // Alerts the same thing
// ^------------^-- "one" and "two" are in an array
为了理解$('#test1').plugin.fun()
在做什么,我们必须看看这个
是如何在JavaScript函数中设置的。我们将从理论开始,然后回到您的插件
在JavaScript中,此
完全由函数的调用方式定义。最常见的方法是将其设置为从对象属性调用函数的副产品:
var foo = {
msg: "I'm foo!",
bar: function() {
alert(this.msg);
}
};
foo.bar(); // alerts "I'm foo!"
该行foo.bar();
有三个不同的功能:
它从foo
对象检索bar
属性
它调用属性引用的函数
它是这样做的#2,this
指的是foo
(更多信息请点击此处。)
所以在电话里
$('#test1').plugin.fun();
…fun
中的将是插件
,因为我们通过插件
上的属性调用fun
您可以考虑使用jQuery UI所使用的机械结构,它有插件的“方法”,可以通过一个main函数来访问,其中的名称是字符串。例如,在jQuery UI >代码>可拖动< /代码>实例中,您可以调用这样的方法:
$('#test1').draggable('disable');
$('#test1').plugin('fun');
有关更多示例,请参见他们的,但基本上您的呼叫如下所示:
$('#test1').draggable('disable');
$('#test1').plugin('fun');
有关函数调用的详细信息:
调用函数时,还有另一种设置此
的方法:通过函数实例上的调用
和应用
函数:
var f = function() {
alert(this.msg);
};
var foo = {
msg: "I'm foo!"
};
f.call(foo); // alerts "I'm foo!"
f.apply(foo); // alerts "I'm foo!" too
call
和apply
调用函数,将this
值设置为传递给它们的第一个参数。call
和apply
之间的区别在于如何将参数传递给目标函数。call
只需向call
函数添加更多参数;使用apply
,您可以将一组参数作为apply
的第二个参数。示例:
function f(arg1, arg2) {
alert(this.msg + " (" + arg1 + ", " + arg2 + ")");
}
var foo = {
msg: "I'm foo!";
};
f.call(foo, "one", "two"); // Alerts "I'm foo! (one, two)"
// ^-- "one" and "two" are discrete arguments
f.apply(foo, ["one", "two"]); // Alerts the same thing
// ^------------^-- "one" and "two" are in an array
fun
函数与调用中使用的对象没有任何直接关系,因此此
将只是窗口
对象
使用$(“#test1”)时.plugin
,您可以获得对该函数的引用,并从中访问函数fun
,但您得到的只是一个常规函数。您也可以使用$.fn.plugin.fun
来访问该函数,一旦获得对的引用,使用jQuery对象和选择器就没有任何关联了ode>乐趣
功能
在调用fun
函数之前,jQuery对象实际上将被丢弃,因此无法从函数中访问它。函数fun
与调用中使用的对象没有任何直接关系,因此此
将只是窗口
对象
使用$(“#test1”)时.plugin
,您可以获得对该函数的引用,并从中访问函数fun
,但您得到的只是一个常规函数。您也可以使用$.fn.plugin.fun
来访问该函数,一旦获得对的引用,使用jQuery对象和选择器就没有任何关联了ode>乐趣
功能
在调用fun
函数之前,jQuery对象实际上会被丢弃,因此不可能从函数中访问它。thnk you T.J.这很有用-我以不同的方式制作了我的插件,我一开始就认为我会这样做