jQuery的一些點滴總結,jQuery點滴總結

來源:互聯網
上載者:User

jQuery的一些點滴總結,jQuery點滴總結

1、動態建立一個全功能的<img>元素:

$('<img>',{src:'jump/img/f_banner.png',alt:'hello img!',title:'just for test',click:function(){alert("hello,img!!!");}}).css({border:'1px solid red',cursor:'pointer',padding:'5px'}).appendTo('body');

2、從封裝集中擷取元素

<pre name="code" class="html">var imgElement=$("img[alt]")[0];   //通過索引擷取元素var imgElement=$("img[alt]").get(0);   //通過方法擷取元素//get()方法也可以接受負的下標,比如.get(-1)返回封裝集中最後一個元素,.get(-2)返回封裝集中倒數第二個元素



3、從封裝集中擷取元素

var imgElement=$("img[alt]")[0];var imgElement=$("img[alt]").get(0);  //可為負值,即倒數第幾個元素


4、以數組形式擷取所有元素

//將label後同層級的所有button元素,將其封裝成jquery封裝器,並建立由這些<button>組成的js數組var allLabeledButtons = $("label + button").toArray();


5、擷取元素的索引  index(element)

//擷取id為findMe的圖片在整個頁面圖片集合中的下標號var n = $("img").index($("img#findMe")[0]);  //可簡寫為:var n = $("img").index("img#findMe")//擷取在其父節點裡的下標號var n = $("img").index();














聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.