[英]Is there a way to access jQuery object from previous chain?
Not sure how I could search for these type of question/answer... 不知道如何搜索这些类型的问题/答案......
This is what I am trying to do... 这就是我想要做的......
(function($){
$.fn.helloworld = {
want: function () {
alert("I want" + this + "!");
}
};
})(jQuery);
Now when I call the function this way, and try to retrieve this
, it will only give me the helloworld
"object". 现在,当我调用该函数这种方式,并试图取回
this
,只会给我helloworld
“对象”。
$("#test").helloworld.want();
Is there a way to access the caller element, #test
, from within? 有没有办法从内部访问调用者元素
#test
?
There's no "nice" way. 没有“好”的方式。 You could do this:
你可以这样做:
var $test = $('#test');
$test.helloworld.want.call($test);
The problem is that by setting up the structure you've got you're essentially forcing the behavior you say you don't want. 问题是,通过设置结构,你已经基本上强迫你说你不想要的行为。
What you could do instead is this: 你可以做的是:
$.fn.helloworld = function( action ) {
var actions = {
test: function() {
alert("Hi!");
},
// ...
};
if (actions[action])
return actions[action].apply(this, [].slice.call(arguments, 1));
return this;
};
Now you can call it: 现在你可以打电话给它:
$('#this').helloworld("test");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.