2016-02-21 41 views
0

這是我的代碼不工作:

$(document).ready(function() { 
 
\t $(document).on('click', '.item', function() { 
 
\t \t $(this).width(120); 
 
\t }); 
 
});
.item { 
 
\t width: 100px; 
 
\t height: 100px; 
 
\t border-radius: 10px; 
 
\t background-color: red; 
 
\t margin-right: 10px; 
 
\t display: inline-block; 
 
\t cursor: pointer; 
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
\t <title>jQuery</title> 
 
\t <link rel="stylesheet" type="text/css" href="style.css"> 
 
</head> 
 
<body> 
 
<div class="item"></div> 
 
<div class="item"></div> 
 
<div class="item"></div> 
 
<div class="item"></div> 
 

 
<script type="text/javascript" src="script.js"></script> 
 
</body> 
 
</html>

在瀏覽器控制檯它說:「沒有定義$ 「一旦頁面加載。問題是什麼?

+0

你忘了在腳本中包含jquery – fbid

+0

未加載jQuery。加載它。 –

回答

2

您必須添加jQuery庫,

你可以在頭部分添加它

或進口的script.js

+0

它工作。謝謝! – Xyntell

2

權利之前在你<head>,你應該導入jQuery庫否則你將無法使用jQuery功能:

<script type="text/javascript" src="path/to/jquery.js"></script> 

您還可以鏈接到它的在線版本:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 

jQuery是一個外部庫,這意味着它不包含在標準的JavaScript功能 - 你需要爲了用上面的方法使其功能可供您的代碼用來導入。

你可以從http://jquery.com/download/下載庫,或者你可以使用在我的第二個例子中的在線版本。如果你從他們的網站上下載你自己的版本,那麼你只需把它放在你的項目中的一個文件夾中,並按照你爲其他腳本所做的相同的方式鏈接到它。

相關問題