Javascript 有没有办法从上一个链访问jQuery对象?

Javascript 有没有办法从上一个链访问jQuery对象?,javascript,jquery,Javascript,Jquery,不知道如何搜索这些类型的问题/答案 这就是我想做的 (function($){ $.fn.helloworld = { want: function () { alert("I want" + this + "!"); } }; })(jQuery); 现在,当我以这种方式调用函数,并尝试检索this,它只会给我helloworld“对象” 有没有办法从内部访问调用者元素test?没有“好”的方法。您可以这样做: var

不知道如何搜索这些类型的问题/答案

这就是我想做的

(function($){
    $.fn.helloworld = {
        want: function () {
            alert("I want" + this + "!");
        }
    };
})(jQuery);
现在,当我以这种方式调用函数,并尝试检索
this
,它只会给我
helloworld
“对象”

有没有办法从内部访问调用者元素test?

没有“好”的方法。您可以这样做:

var $test = $('#test');
$test.helloworld.want.call($test);
问题是,通过建立你已经拥有的结构,你实际上是在强迫你说你不想要的行为

你可以做的是:

$.fn.helloworld = function( action ) {
  var actions = {
    test: function() {
      alert("Hi!");
    },
    // ...
  };

  if (actions[action])
    return actions[action].apply(this, [].slice.call(arguments, 1));
  return this;
};
现在你可以称之为:

$('#this').helloworld("test");

这个怎么样?是否可以创建一个围绕jQuery对象的包装器对象?所以我有我自己的函数,它位于jQuery之上,当我需要调用某个东西时,我可以通过传递它来“继承”jQuery功能?例如,$helloworld(“#test”).test();当实现时,我的自定义对象变成
$helloworld
,类似于jQuery的
$
jQuery
@codenamezero,您可以使用
$helloworld=object.create($.prototype)
使用jQuery原型创建对象。当然,您不会直接在对象上获取像
$.get()
这样的函数,但是您也可以复制它们。不过,我不明白这有什么意义;您仍然必须与所有其他jQuery函数共享同一名称空间。
$('#this').helloworld("test");