简体   繁体   English

为什么这个jQuery扩展不起作用?

[英]Why is this jquery extension not working?

I have this extension function and it seems to be not causing any problems. 我有此扩展功能,它似乎没有引起任何问题。 But as soon as I try to use it, I get an error message: Object doesn't support property or method 'n2name' 但是,一旦我尝试使用它,就会收到一条错误消息:对象不支持属性或方法“ n2name”

错误的屏幕截图

No errors are reported in the plug-in code (snippet pasted below for reference, with a link to the full file at the end). 插件代码中未报告任何错误(下面粘贴的片段供参考,最后指向完整文件的链接)。

Does anyone have any idea why this isn't working? 有谁知道为什么这不起作用?

Note: I'm trying to fix an issue on this open-source project: https://github.com/n2cms/n2cms/issues/279 and there may be more useful details on that Github bug. 注意:我正在尝试解决此开源项目上的问题: https : //github.com/n2cms/n2cms/issues/279,并且可能有关于Github错误的更有用的详细信息。

Sorry if this question is too vague, and for the long code snippet (most of it probably isn't relevant). 很抱歉,如果这个问题太含糊,对于较长的代码段(大多数可能无关紧要)。 But any help would really be appreciated 但任何帮助将不胜感激

/**
* n2name 0.2
*/

(function($) {

    /* some code removed for brevity */

    $.fn.n2name = function(options) {
        var invokeUpdateName = function(){
            updateName(options.titleId, options.nameId, options.whitespaceReplacement, options.toLower, options.replacements, options.keepUpdatedBoxId);
        };
        if(options.keepUpdatedBoxId){
           /* more code removed for brevity */
        }
    };
})(jQuery);;

Link to jquery.n2name.js full source, if the code snippet above is not useful: https://github.com/n2cms/n2cms/blob/4469580fcdd9c91f7576f07c3d2c8a4479ed6ce9/src/Mvc/MvcTemplates/N2/Resources/Js/plugins/jquery.n2name.js 如果上面的代码段没有用, 链接到jquery.n2name.js的完整源代码: https : //github.com/n2cms/n2cms/blob/4469580fcdd9c91f7576f07c3d2c8a4479ed6ce9/src/Mvc/MvcTemplates/N2/Resources/Js/plugins/jquery .n2name.js

The errors "Object doesn't support property or method <name> " (IE) and " <name> is not a function" (Chrome) usually mean that your jQuery plugin didn't successfully load before it was called. 错误“对象不支持属性或方法<name> ”(IE)和“ <name>不是函数”(Chrome)通常表示您的jQuery插件在调用之前未成功加载。

This can be due to a failed request (check Dev Tools' Network tab), forgetting/misplacing the plugin's script tag or the script tag having defer / async attributes. 这可能是由于失败的请求(检查开发工具网络选项卡),忘记/乱放插件的script标签或script有标签defer / async属性。 Another possible cause is the plugin's script containing syntax errors (check the Dev Tools' Console tab in that case). 另一个可能的原因是插件的脚本包含语法错误(在这种情况下,请检查“开发工具”的“控制台”选项卡)。

Having multiple versions of jQuery in the same page (as commented in OP's linked GH issue) is another possibility, seeing as plugins wrapped in the (function(){}(jQuery)); 在同一个页面中具有多个版本的jQuery(如OP的链接的GH问题中所述)是另一种可能,可以看到插件被包装在(function(){}(jQuery)); boilerplate are only added to the jQuery object referenced by window.jQuery at the time the plugin script loads. 样板仅在插件脚本加载时添加到window.jQuery引用的jQuery对象中。


In this specific case, OP was having issues to load the plugin from localhost inside VS. 在这种情况下,OP在从VS内部的localhost加载插件时遇到了问题。

I've seen similar issues, and I'll just add that it is invalid to use protocol-less URLs (for example: //somecdn.com/library.js ) when loading files from a file:/// URI scheme. 我已经看到了类似的问题,并且我只是补充说,从file:/// URI方案加载文件时,使用无协议的URL(例如: //somecdn.com/library.js )是无效的。 This is because protocol-less URLs "inherit" the protocol being used from the parent document and file:/// is not a protocol. 这是因为无协议URL“继承”了父文档和file:///中使用的协议,而不是协议。 In case you're loading it from a local server (eg parent document served through http as in http://localhost/library.js ) the protocol-less URLs should work fine. 如果您是从本地服务器(例如,通过http父文档,如http://localhost/library.js )加载的, http://localhost/library.js协议的URL应该可以正常工作。

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

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