JQuery的元素操作函式

2021-08-26 02:46:37 字數 1810 閱讀 2749

no.方法

語法描述備註1

文字內容成為選擇器選中的元素的最後乙個子節點

content可以是文字,html元素,jquery物件,dom物件

2元素或文字內容新增成為選擇器選中的元素的最後乙個子節點

content可以是選擇器,也可以文字內容,若是文字內容,必須有html標記,使之成元素。

3prepend()

$(selector).prepend(content)

文字內容成為選擇器選中的元素的第乙個子節點同14

prependto()

$(content).prependto(selector)

元素或文字內容新增成為選擇器選中的元素的第乙個子節點同25

after()

$(selector).after(content)

元素或文字內容新增成為選擇器選中的元素的下乙個兄弟節點同16

before()

$(selector).before(content)

元素或文字內容新增成為選擇器選中的元素的上乙個兄弟節點同1

div1

div2

div3

div4

執行結果:

示例2:after()和before()

div1

div2

div3

div4

執行結果:

no.方法語法

描述備註

1clone()

$(selector).clone()

將元素轉殖乙份,包含屬性和內容

2wrap()

使用指定的html元素來包裹被選元素

3unwrap()

$(selector).unwrap()

移除被選元素的父元素

4empty()

$(selector).empty

清空元素的內容(即元素的所有子節點和孫子節點和內容)

5replacewith()

$(selector).replacewith(content)

將content代替被選中的元素

content可以是html 元素、jquery 物件、dom 元素。如果是已存在的元素物件,那麼原來的將被移除。

示例:

div1

div2

div4

div6

這是第乙個段落。

這是第二個段落。

這是第三個段落。

$("#test2").wrap("")

$("#test1").wrap($("#test2"))

$("#test4").unwrap()

$("#test5").empty()

$("p:nth-of-type(1)").replacewith($("p:nth-of-type(2)"))

$("p:last").replacewith("hello")

執行結果:

jQuery操作元素

對於元素屬性的操作 attr 屬性名 獲得屬性 如 a attr href 獲得鏈結的內容 removeattr 屬性名 刪除此屬性 如 a removeattr href 移除href屬性 2.對於元素內容的操作 dd html 獲得元素的html內容 dd html 哈哈哈 設定元素的html內...

jQuery操作元素

獲取內容 test text test html test val 獲取屬性 test attr href 設定或者修改屬性 attr disabled disabled removeattr checked input select在value值變化時可以呼叫不同的函式 change functi...

jQuery 元素操作

一.高度和寬度 獲取高度 div height 高度 div width 寬度 設定高度 div height 300 div height 300px 有沒有單位都可以 獲取高度和寬度,沒有引數 結果300 number型別 如果要進行數 算,要用這個方法 二.座標值 獲取位置值的兩種方式 1 o...