简体   繁体   English

在点击事件处理程序中调用自定义jQuery插件会生成错误

[英]Calling custom jQuery plugin inside click event handler generates error

I've been racking my brain on this issue. 在这个问题上,我一直在绞尽脑汁。 I've written a few custom plugins and I've never run into this before. 我已经编写了一些自定义插件,而我以前从未遇到过。

Here is my scenario. 这是我的情况。 I have written a simple plugin. 我写了一个简单的插件。 Then within a click event handler I want to call my plugin. 然后在点击事件处理程序中,我想调用我的插件。 When I do this I get an error that says "Object doesn't support property or method 'plugin name'". 当我这样做时,我得到一个错误,提示“对象不支持属性或方法'插件名称'”。

In order to troubleshoot this I removed all other scripts besides jQuery and jQuery UI and my custom plugin. 为了解决此问题,我删除了除jQuery和jQuery UI以及我的自定义插件以外的所有其他脚本。 That still did not fix the issue. 那仍然没有解决问题。 So thinking maybe I was having a moment I copied the example plugin from the jQuery page 'greenify' and tested using this. 因此,我想也许我有一会儿从jQuery页面“ greenify”复制了示例插件,并对此进行了测试。 Still the same result. 结果还是一样。 Below is an example of what I'm trying to do. 以下是我要执行的操作的示例。

Note: The odd issue is that when I call the plugin outside of the 'click' event handler it works just fine. 注意:奇怪的问题是,当我在“ click”事件处理程序之外调用插件时,它工作正常。

Plugin: 插入:

(function ($){
    $.fn.greenify = function(options){
        // This is the easiest way to have default options.
        var settings = $.extend({
            // These are the defaults.
            color: "#556b2f",
            backgroundColor: "black"
        }, options );
       // Greenify the collection based on the settings variable.
       return this.css({
        color: settings.color,
        backgroundColor: settings.backgroundColor
    });
};
}( jQuery ));

Then two examples of calling the plugin 1) outside the event and 2) inside the event: 然后是在事件外部调用插件1)在事件内部调用插件的两个示例:

<script type="text/javascript" src="mypluginloaction.js"></script>
<script type="text/javascript">
  $(function(){

    //This works!
$('a').greenify();


$('.post-box-left').on('click', function(){
            //This does not work
    $('a').greenify();
    $('#blurBackground').show();
    $('#popupWindow').show();
});
</script>

What is really annoying is that when I jsFiddle it the plugin works. 真正令人讨厌的是,当我用jsFiddle插件运行时。 What I'm looking for is suggestions on why this could be happening. 我正在寻找有关为什么会发生这种情况的建议。 I'll try anything. 我会尝试的。 Thanks! 谢谢!


UPDATE: 更新:

After racking my brain some more I found what my issue was but I'm not sure 100% how to solve it. 绞尽脑汁后,我发现了问题所在,但不确定100%如何解决。

In another $(function(){}); 在另一个$(function(){})中; I call $().load() to load an ajax page. 我调用$()。load()来加载ajax页面。 If I remove it then everything works great. 如果我删除它,那么一切都会很好。

$(function(){
    /* Removing this makes it work!
        $('#Poll').load('AJAX/Poll.asp');
    */
});

Any idea why? 知道为什么吗?

I was able to find the root cause. 我找到了根本原因。 I was pulling in an page through ajax (written by someone else) that had a call to another jQuery instance. 我正在通过ajax(由其他人编写)进入一个页面,该页面调用了另一个jQuery实例。 Since this call was after my include of the plugin and before the call to my code it was overwriting the jQuery instance. 由于此调用是在我包含插件之后且在调用我的代码之前,因此它覆盖了jQuery实例。 Which I'm guessing is why it ran during document ready but not inside a jQuery collection. 我猜这是为什么它在文档准备期间运行但不在jQuery集合内运行的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM