2013-04-10 68 views
1

在铬我userscript导致错误jquery/userscript没有定义?

ReferenceError: jQuery is not defined 

但行@require包括jQuery的。至少我知道它与greasemonkey(铬不支持吗?)

如果我删除函数调用(这使$ = jQuery)的函数,我得到错误$未定义。在Firefox/Greasemonkey的运行良好

// ==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); 
+0

我想你忘了在项目中加入jquery – gaurav 2013-04-10 13:24:50

+0

你是否已经将它包含在你的页面中?例如:'

0

检查两个最重要的事情:

  1. 您已经包括jQuery的在您的项目
  2. 您已经包括了使用jQuery的
  3. 任何其他js文件之前,jQuery的文件
+1

@ require包含jquery。至少它与greasemonkey有关 – BruteCode 2013-04-10 13:31:18