深入淺析jQuery對象$.html,jquery.html

來源:互聯網
上載者:User

深入淺析jQuery對象$.html,jquery.html

$對象

  說起jQuery,最明顯的標誌,毫無疑問,就是, ,其實是jquery的簡寫。而使用$()封裝的對象就是jQuery對象

  與jQuery對象相對應的就是DOM對象,DOM對象其實就是DOM元素節點對象

  如果直接寫document,則指的是document的DOM元素對象

document.onclick = function(){alert('dom');}

  而如果用()包括起來,如 ()包括起來,如(document),是jQuery(document)的簡寫形式,則指的是jQuery對象

<script src="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="jquery-3.1.0.js"></script> <script>//無反應$(document).onclick = function(){alert(0);};//Uncaught TypeError: document.click is not a functiondocument.click(function(){alert(1);});</script>

轉換

【1】DOM轉jQuery對象

  對於一個jQuery對象,只需要用$()把DOM對象封裝起來,就可以獲得一個jQuery對象

【2】jQuery轉DOM對象

  jQuery是一個類數組對象,可以通過[index]或get(index)的方法得到相應的DOM對象

console.log(document === $(document)[0]);//trueconsole.log(document === $(document).get(0));//true

 共存

  如果jQuery對象和DOM對象指向同一對象,綁定不同函數,則函數會按照順序依次執行

//先彈出0,再彈出1document.onclick = function(){alert(0);}$(document).click(function(){alert(1);});

以上所述是小編給大家介紹的jQuery對象$.html,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對幫客之家網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.