首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jquery/userscript是否未定义?

jquery/userscript是否未定义?
EN

Stack Overflow用户
提问于 2013-04-10 21:22:57
回答 2查看 4.2K关注 0票数 1

在chrome中,我的userscript导致错误

代码语言:javascript
复制
ReferenceError: jQuery is not defined

但是@require这一行包含jquery。至少我知道greasemonkey是这样的( chrome不支持吗?)

如果我删除一个函数调用的函数(使$= jQuery),我会得到错误$ is not defined。在firefox/greasemonkey中它运行正常

代码语言:javascript
复制
// ==UserScript==
// @name        Detect Duplicate IDs
// @namespace   jkbfvsdjkzsvfshefsdvh
// @description Alerts you when more than one ID is on a page
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @include     *
// @version     1
// @grant
// ==/UserScript==

//using localhost in @include doesn't seem to work
if(location.hostname!='localhost')
    return;

//Ignore above for console.
(function($){
$(function() {
var checkDupeIDs = function () {
    var dupes = [];
    var ids = [];
    $('[id]').each(function(i,e){ids.push(e.id)});
    var len = ids.length;
    for(i=0; i<len; ++i) {
        if(dupes.indexOf(ids[i])!=-1)
            continue;
        for(n=i+1; n<len; ++n) {
            if (ids[n]==ids[i]) {               
                dupes.push(ids[n]);
                break;
            }
        }
    }
    if (dupes.length!=0) { 
        for(i=0; i<dupes.length; ++i)
            console.warn('Multiple IDs #' + dupes[i]);
        alert(dupes.join('\n')); 
    }
}


var jHtml = $.html;
$.html = function () {
    checkDupeIDs();
    return jHtml.call(this, arguments);
}

checkDupeIDs();

})
})(jQuery);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-10 21:59:19

直接开箱即用,Chrome不能很好地支持Greasemonkey脚本(Out-of-date tableOut-of-date page)。例如,@require指令不起作用。

要享受Firefox Greasemonkey脚本几乎完全的兼容性,请安装the Tampermonkey Extension for Chrome。你的脚本应该能在坦帕猴中工作。

此外,为了避免在火狐中出现问题,不要将@grant留空。如果不使用其他方法,请使用@grant  GM_addStyle

票数 2
EN

Stack Overflow用户

发布于 2013-04-10 21:28:55

检查两个最重要的东西:

  1. 您在项目中包含了jQuery
  2. 在使用jQuery

的任何其他js文件之前包含了jQuery文件

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15927113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档