2011-10-18 34 views
1

我試圖將jQuery添加到我的一個項目中,但無法使其工作。CakePHP示例中的簡單jQuery不起作用

/views/layouts/default.ctp

echo $this->Html->script('jquery-1.6.4.js'); 
echo $this->Html->script('test.js'); 

/view/test/view.ctp

<?php echo $html->link('click msg', '#', array('onclick'=>'return false;', 'id'=>'msg-name', 'class'=>'msg-link')); ?> 

/views/layouts/js/test.js

$(document).ready(function() 
{ 
    $('.msg-link').click(function() 
    { 
     alert("Thanks for clicking."); 
    }); 
}); 

當我點擊按鈕/鏈接時,它應該彈出警報,但它不會做任何事情:(

而且我是鍍鉻的調試控制檯我看到這個錯誤:

/js/jquery-1.6.4.js:-1 Resource interpreted as Script but transferred with MIME type text/html. 
/js/test.js:-1 Resource interpreted as Script but transferred with MIME type text/html. 
jquery-1.6.4.js:1 Uncaught SyntaxError: Unexpected token < 
test.js:1 Uncaught SyntaxError: Unexpected token < 

林真的不知道他們的意思,不能看股票查詢如何能在它的錯誤。

任何人都可以幫忙嗎?

謝謝!

回答

5

你必須JS文件複製到/根目錄/ JS /。

然後,你必須在你的CTP文件中設置這樣的:

echo $this->Html->script('jquery-1.6.4'); 
echo $this->Html->script('test'); 
+0

嗨Chalist,謝謝:)我剛剛發現了這個......除非我錯過了它,它不是很有據可查,必須放在/ webroot/js /而不是放在/ views/layouts/js / – Tony

5

刪除.js擴展名。

echo $this->Html->script('jquery-1.6.4'); 
echo $this->Html->script('test'); 

This method of javascript file inclusion assumes that the javascript file specified resides inside the /app/webroot/js directory.

http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html?highlight=html#HtmlHelper::script

+0

哎呦:)你是第一:) – user973254

+0

嗨,感謝您的答覆,已嘗試兩者不作區別,它正確鏈接的html /js/jquery-1.6.4.js當我有.js和沒有:( – Tony