簡體   English   中英

$(document).ready(function()不起作用

[英]$(document).ready(function() doesn't work

我在HTML代碼內的jQuery代碼有問題。

如果我在HTML代碼中編寫以下代碼,則該代碼可以正常運行(出現警告,提示“ hello world”):

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery.js"> </script>
<script>
      alert("hello world");
</script>
</head>
</html>

但是,當我編寫以下代碼(使用$(document).ready函數)時,它不起作用,我也不知道為什么:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery.js"> </script>
<script>
$(document).ready(function() {
alert("hello world");
});
</script>
</head>
</html>

有人可以告訴我這是怎么回事嗎?

提前致謝。

編輯:很抱歉缺少括號,這只是一個錯字

您在jQuery的<script>標記中缺少結束符>

<script type="text/javascript" src="jquery.js"> </script>
                                              ^
 missing bracket here ------------------------|

您在腳本標簽中缺少引號和開始標簽的結束> ,這意味着它已損壞,因此腳本無法加載。

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

您編輯的問題將顯示更正的script標記。 如果仍然無法使用,則您通往jquery庫的路徑可能是錯誤的。

編碼時,應保持瀏覽器的開發人員控制台處於打開狀態。 它將顯示正在發生的錯誤。 可能有一個ReferenceError指出$undefined

包含jQuery的<script>標記格式錯誤:

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

它看起來應該像這樣:

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

假設jQuery庫位於與HTML頁面相同目錄中的名為“ jquery.js”的文件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM