欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

鋒利的jQuery jQuery中的DOM操作

 更新時(shí)間:2010年03月21日 12:14:50   作者:  
鋒利的jQuery 要點(diǎn)歸納(二)上 jQuery中的DOM操作,需要的朋友可以參考下。

1 查找元素節(jié)點(diǎn)

    var $x = $("selector").text()

2 查找屬性節(jié)點(diǎn)

    var $x = $("selector").attr("property")

3 創(chuàng)建節(jié)點(diǎn)

    var $x = $("html")

4 插入節(jié)點(diǎn)

$("selector").append()
    向每個(gè)匹配的元素內(nèi)部追加內(nèi)容
$("selector").appendTo()
    等價(jià)于.append()操作符左右互換

$("selector").prepend()
    向每個(gè)匹配的元素內(nèi)部前置內(nèi)容
$("selector").prependTo()
    等價(jià)于.prepend()操作符左右互換

$("selector").after()
    在每個(gè)匹配的元素之后插入內(nèi)容
$("selector").insertAfter
    等價(jià)于.after()操作符左右互換

$("selector").before()
    在每個(gè)匹配的元素之前插入內(nèi)容
$("selector").insertBefore()
    等價(jià)于.before()操作符左右互換

5 移動(dòng)節(jié)點(diǎn)

本書(shū)P70例:

復(fù)制代碼 代碼如下:

<script>
var $one_li = $("ul li:eq(1)"); //獲取<ul>節(jié)點(diǎn)中第2個(gè)<li>元素節(jié)點(diǎn)
var $two_li = $("ul li:eq(2)"); //獲取<ul>節(jié)點(diǎn)中第3個(gè)<li>元素節(jié)點(diǎn)
$two_li.insertBefore($one_li); //移動(dòng)節(jié)點(diǎn)
</script>


6 刪除節(jié)點(diǎn)

6.1 remove()方法

$("selector").remove()
    remove()方法將刪除selector所有后代節(jié)點(diǎn),元素用remove()方法刪除后,仍可以繼續(xù)使用。另外remove()方法也可以通過(guò)傳遞參數(shù)

來(lái)選擇性地刪除元素,如$("ul li").remove("li[title!=xxx]");

6.2 empty()方法

$("selector").empty()
    清空selector的所有后代節(jié)點(diǎn)

7 復(fù)制節(jié)點(diǎn)

$("selector").clone()
    如$(this).clone().appendTo("ul")。若要使復(fù)制后的新元素帶有原元素所擁有的行為,需要傳遞參數(shù)true。如$("selector").clone(true)

8 替換節(jié)點(diǎn)

$("selector").replaceWith()
    將所有匹配的元素都替換成指定的HTML或者DOM元素
$("selector").replaceAll()
    等價(jià)于.replaceWith()操作符左右互換

9 包裹節(jié)點(diǎn)

$("selector").wrap()
    將所有匹配的元素單獨(dú)包裹
$("selector").wrapAll()
    將所有匹配的元素用一個(gè)元素包裹
$("selector").wrapInner()
    將每一個(gè)匹配的元素的子內(nèi)容(包括文本節(jié)點(diǎn))用其他結(jié)構(gòu)化的標(biāo)記包裹起來(lái)

10 屬性操作

$("selector").attr()
    獲?。ㄒ粋€(gè)property參數(shù))和設(shè)置元素屬性(兩個(gè)參數(shù),property和value),如$("p").attr("title","your title")。如果同時(shí)設(shè)

置多個(gè)屬性,格式如$("p").attr({"title" : "your title" , "name" : "test"})
$("selector").removeAttr()
    刪除元素屬性

11 樣式操作

$("selector").attr()
    替換樣式

$("selector").addClass()
    追加樣式

$("selector").removeClass()
    移除樣式

$("selector").toggle()
    行為重復(fù)切換
例:

復(fù)制代碼 代碼如下:

<script>
$x.toggle(function(){
//code1
},function(){
//code2
})
</script>

交替執(zhí)行code1和code2

$("selector").toggleClass()
    控制樣式上的重復(fù)切換,如$("p").toggleClass("anotherClass")

$("selector").hasClass("anotherClass")
    判斷selector中是否含有anotherClass

相關(guān)文章

最新評(píng)論