2013-03-28 31 views
1

我此腳本:

<script> 
    $(document).ready(function() { 
     $('a[@href^= ""] img').parent().click(function() { 
      var linkz = $(this).attr("href"); 
      if(linkz.toLowerCase().indexOf("http: //www.website.com") >= 0) { 
       window.open($(this).attr("href")); 
       return false; 
      } else { 
       window.open("http://www.website.com/p/img.html?img=" + $(this).attr("href ")); 
       return false; 
      } 
     }); 
    }); 
</script> 

要傳遞圖像URL的新鏈接的新頁面打開的所有圖像。但我發現

TypeError: $ is not a function.

我試圖添加的,而不是$(文件)的jQuery(文檔),但後來我得到了

$(&#39;a[@href^=&quot;&quot;] img&#39;) TypeError: $ is not a function

這裏。

+5

「$不是一個函數」通常,你已經忘了包括jQuery庫 – 2013-03-28 01:59:57

+0

一個明確的信號,你怎麼包括jQuery的進頁? – 2013-03-28 02:00:07

回答

0

看起來你沒有正確包含jQuery,因爲瀏覽器不知道$是什麼。請確保你有這樣的事情在<head>包括jQuery的:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script> 
1

你要麼不包括jQuery的,否則你跑noConflict(),它發佈$的控制權。

http://api.jquery.com/jQuery.noConflict/

如果使用noConflict,你只需要在整個使用jQuery(),包括jQuery的( 'A [@^HREF = 「」] IMG')。

0

你還沒有加入jQuery的在<head></head>

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> 

添加這個,如果你是在本地測試,和你有一個llow帶寬,然後下載腳本一次** http://code.jquery.com/jquery-1.9.1.min.js **給你的文件夾和 使用
<script src="jquery-1.9.1.min.js"></script>

0

試試這個;

<script language="JavaScript" type="text/javascript" src="jquery/jquery.js"></script> 
<script> 
    jQuery.noConflict(); 
    (function ($) { 
     function readyFn() { 
      // Set your code here!! 
     } 

     $(document).ready(readyFn); 
    })(jQuery); 

</script> 

或者你的情況:

<script language="JavaScript" type="text/javascript" src="jquery/jquery.js"></script> 
<script> 
    jQuery.noConflict(); 
    (function ($) { 
     function readyFn() { 
      $('a[@href^= ""] img').parent().click(function() { 
      var linkz = $(this).attr("href"); 
      if(linkz.toLowerCase().indexOf("http: //www.website.com") >= 0) { 
       window.open($(this).attr("href")); 
       return false; 
      } else { 
       window.open("http://www.website.com/p/img.html?img=" + $(this).attr("href")); 
       return false; 
      } 
     }); 
     } 

     $(document).ready(readyFn); 
    })(jQuery); 

</script> 
相關問題