jquery對(duì)象訪問(wèn)

字號(hào):


    本節(jié)主要將對(duì)象訪問(wèn):each(),size(),length,selector,context,get(),get(index)。弄懂通過(guò)這幾個(gè)屬性或者方法的對(duì)象訪問(wèn)是本節(jié)的重點(diǎn)。
    下面定義一個(gè)公共的html代碼
    code:
    <!doctype html public -//w3c//dtd html 4.0 transitional//en>
    <html>
    <head>
    <title></title>
    <script language=javascript src=jquery-1.4.2.min.js></script><!--引入jquery框架,目前jquery-1.4.2.min.js是最高版本-->
    <script language=javascript>$(function(){
    <!--jquery文檔處理代碼區(qū)-->
    });</script>
    </head>
    <body>
    <div id=foraspcn><p>網(wǎng)站制作學(xué)習(xí)網(wǎng)</p></div>
    <p>jquery第一課,jquery核心函數(shù)$選擇符</p>
    <img id=image1 src=test1.jpg/> <img src=test2.jpg/><網(wǎng)站制作學(xué)習(xí)網(wǎng)foasp.cn>
    <form action= method=get name=form1>
    <p>forasp.cn</p>
    </form>
    <form action= method=get name=form2>
    <p>www.forasp.cn</p>
    </form>
    </body>
    <html>
    1. 對(duì)象.each(function(){});對(duì)象或者對(duì)象數(shù)組中的每個(gè)對(duì)象進(jìn)行循環(huán)操作function(i);這里的i是索引,從0開始一直到p的個(gè)數(shù)減1
    舉例:
    $(p).each(function(){$this.innerhtml=網(wǎng)站制作學(xué)習(xí)網(wǎng)+i;});或者$(p).each(function(i){$(this).append(網(wǎng)站制作學(xué)習(xí)網(wǎng)+i);});//結(jié)果是每個(gè)p的內(nèi)容替換為網(wǎng)站制作學(xué)習(xí)網(wǎng)i(p的索引)
    2.對(duì)象.size() 返回對(duì)象數(shù)組的個(gè)數(shù),這個(gè)跟length的功能是一樣的。
    舉例$(form).size(); 結(jié)果是2 也就是2個(gè)form
    3.length 跟size()的效果是一樣的。
    舉例 $(form).length; 輸出 2
    4.對(duì)象。selector 確定查詢的選擇器。也就是要確定要選擇的內(nèi)容。
    舉例$(form).selector;則返回form。
    5.對(duì)象.context 檢測(cè)使用的文檔內(nèi)容,比如是字符串,是object,還是其他內(nèi)容等。
    $(form).context; 輸出[object htmldocument]  //如果是ie瀏覽器,則返回[object]
    6.get(),返回的是dom對(duì)象取得所有匹配的 dom 元素集合,不是jquery對(duì)象集合
    舉例:$(img).get().reverse();結(jié)果:[ <img src=test2.jpg/> <img src=test1.jpg/> ]$(img).get()返回的是數(shù)組。
    7.get(index),返回的是dom對(duì)象取得所有匹配的 dom 元素集合,不是jquery對(duì)象集合不過(guò)有了index索引
    舉例:$(img).get(0).src;結(jié)果:text1.jpg
    8.index(對(duì)象) 返回對(duì)象的索引值
    舉例 $(img)index($image1);//輸出0,即第一個(gè)圖片的索引值
    以上就是jquery的對(duì)象訪問(wèn)