$對象
說起jQuery,最明顯的標志,毫無疑問,就是, ,其實是jquery的簡寫。而使用$()包裝的對象就是jQuery對象
與jQuery對象相對應的就是DOM對象,DOM對象其實就是DOM元素節(jié)點對象
如果直接寫document,則指的是document的DOM元素對象
document.onclick = function(){ alert('dom'); }
而如果用()包括起來,如 ()包括起來,如(document),是jQuery(document)的簡寫形式,則指的是jQuery對象
<script src="http://www.gimoo.net/t/1901/jquery-3.1.0.js"></script> <script> console.log(jQuery(document));//[document] console.log($(document));//[document] console.log(document);//#document </script>
[注意]jQuery對象無法使用DOM對象的方法,DOM對象也無法使用jQuery對象的方法
<script src="http://www.gimoo.net/t/1901/jquery-3.1.0.js"></script> <script> //無反應 $(document).onclick = function(){ alert(0); }; //Uncaught TypeError: document.click is not a function document.click(function(){ alert(1); }); </script>
轉換
【1】DOM轉jQuery對象
對于一個jQuery對象,只需要用$()把DOM對象包裝起來,就可以獲得一個jQuery對象
【2】jQuery轉DOM對象
jQuery是一個類數(shù)組對象,可以通過[index]或get(index)的方法得到相應的DOM對象
console.log(document === $(document)[0]);//true console.log(document === $(document).get(0));//true
共存
如果jQuery對象和DOM對象指向同一對象,綁定不同函數(shù),則函數(shù)會按照順序依次執(zhí)行
//先彈出0,再彈出1 document.onclick = function(){ alert(0); } $(document).click(function(){ alert(1); });
以上所述是小編給大家介紹的jQuery對象$.html,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對綠夏網(wǎng)網(wǎng)站的支持!