Javascript jQuery插件-文档为空-jQuery调用createSafeFrag()
我正在编写一个jQuery插件,我希望有多个功能可以像这样使用:Javascript jQuery插件-文档为空-jQuery调用createSafeFrag(),javascript,jquery,jquery-plugins,Javascript,Jquery,Jquery Plugins,我正在编写一个jQuery插件,我希望有多个功能可以像这样使用: //Calling the "constructor" $(element).pluginName(); //Calling the "method" $(element).pluginName.methodName(); 所以我基本上是这样做的: (function($, window, document, undefined) { //The "constructor" $.fn.pluginName =
//Calling the "constructor"
$(element).pluginName();
//Calling the "method"
$(element).pluginName.methodName();
所以我基本上是这样做的:
(function($, window, document, undefined) {
//The "constructor"
$.fn.pluginName = function() {
//try accessing $(this)
var meh = $(this);
};
//The "method"
$.fn.pluginName.methodName = function() {
//try accessing $(this)
var test = $(this);
};
})(jQuery)
现在,当我像上面框中描述的那样调用它时,它对“构造函数”起作用。但当我尝试“方法”时,我得到一个错误:
TypeError: document is null
http://(url)/jquery.js
safeFrag = document.createDocumentFragment();
Line 5823
现在来看有趣的部分:当我将“方法”重命名为$.fn.pluginNameMethodName
(因此,基本上,如果我删除最后一个
),我可以通过调用$(element.pluginNameMethodName())来调用“方法”代码>
那么,我做错了什么
我希望我的插件有多个易于访问的方法(我不希望我的插件方法被$(element.pluginName(methodName);
)。如果你想要易于访问的方法,让你的插件提供一个存储在元素上的接口
(function($, window, document, undefined) {
//The "constructor"
$.fn.pluginName = function() {
return this.each(function(){
var obj = {
elem: this,
theMethod: function(){
var meh = this.elem;
}
};
$(this).data("pluginName",obj);
})
};
})(jQuery)
// using it
var pluginInterface = $(theelement).pluginName().data("pluginName");
pluginInterface.theMethod();
但是,我考虑<代码> $(元素)。代码>也很容易访问。(甚至更多)。
检查你的这个。不过,我认为您无法解决此问题。请您更具体一点,好吗?由于methodname是$.fn.pluginName而不是$.fn.的属性,您无法轻松访问该元素。methodName无法访问相应的$实例。谢谢您的回答。我明天会试试这个!因此,经过一些尝试,我决定使用$(元素)代码>符号。谢谢你!