2011-12-25 23 views
3

什麼是確定網頁是否啓用jQuery的最佳方式?如果這是確定它的最好方法,那麼使用jquery本身。如何確定網頁是否啓用jQuery?

+0

? – Virendra 2011-12-25 20:08:20

+2

什麼是「jQuery-enabled」應該是什麼意思? – 2011-12-25 20:08:29

回答

9
if(jQuery) //jquery object exists 

jQuery是不是魔術 - 它本質上只是一個大對象。您可以像檢查其他任何對象一樣檢查它。

,以確保內的jQuery庫同樣的事情被載入:

if(jQuery.DatePicker) //lib exists 
+1

乾杯,沒有在幾個月內使用JavaScript或jQuery,所以有點生疏。 – 2011-12-25 20:11:21

+1

如果jQuery沒有啓用,它會拋出一個錯誤,並停止執行腳本。 – Rich 2016-05-14 08:18:49

2

如果jquery對象存在,請檢入javascript。

使用下面的if條件來檢查jQuery對象是否存在。

if(jQuery) 
{ 
//jquery object exists 
} 
+0

更有用的答案應該包含一個代碼示例。 – rkb 2011-12-26 00:08:10

1

做一次檢查,看看是否JavaScript對象被初始化。

if(jQuery) 
{ 
    alert('jquery active'); 
} 
6

,以檢查是否jQuery是加載的最佳方式是

if (window.jQuery) { 
    // jQuery is loaded 
} else { 
    // jQuery is not loaded 
} 

如果選中使用if(jQuery){},並且它不存在,你會得到一個引用錯誤像下面,和它會破壞你的腳本的執行。通過檢查窗口對象是否有一個名爲jQuery的屬性,如果它不存在,它將簡單地返回undefined。

enter image description here

1

運行此控制檯:

if (window.jQuery) { 
    console.log("Yes there's jQuery!"); 
} else { 
    console.log("Nope, it's not on this site..."); 
}; 
要檢查網頁已有包括jQuery庫
相關問題