jQuery 文件處理

2022-07-15 16:42:15 字數 2024 閱讀 4408

文件處理

1.內部插入

2.1 after(content|fn) 在每個匹配的元素之後插入內容

$("p").after("after");

$("p").after(function(i,html));

2.2 before(content|fn) 在每個匹配的元素之前插入內容

$("p").after( $("#id")[0] );

$("p").after( $("span") );

2.3 insertafter(content) 把所有匹配的元素插入到另乙個、指定的元素元素集合的後面。

$("insertafter").insertafter("p");

2.4 insertbefore(content) 把所有匹配的元素插入到另乙個、指定的元素元素集合的前面。

$("insertbefore").insertbefore("p");

3.包裹

3.1 wrap(html|element|fn) 把所有匹配的元素用其他元素的結構化標記包裹起來。

$("p").wrap("

");$("p").wrap(document.getelementbyid('content'));

$(".wrap").wrap(function())

3.2 unwrap() 這個方法將移出元素的父元素。

$("p").unwrap();

3.3 wrapall(html|elem) 將所有匹配的元素用單個元素包裹起來

$("p").wrapall("div");

3.4 wrapinner(html|element|fn) 將每乙個匹配的元素的子內容(包括文字節點)用乙個html結構包裹起來

$("p").wrapinner("");

$("p").wrapinner(document.createelement('b'))

$(".inner").wrapinner(function())

4.替換

4.1 replacewith(content|fn) 將所有匹配的元素替換成指定的html或dom元素

$("p").replacewith("replacewith");

$("p").replacewith(function());

4.2 replaceall(selector) 用匹配的元素替換掉所有 selector匹配到的元素。

$("replaceall").replaceall("p");

5.刪除

5.1 empty() 刪除匹配的元素集合中所有的子節點。

$("p").empty()

5.2 remove([expr]) 從dom中刪除所有匹配的元素,這個方法不會把匹配的元素從jquery物件中刪除,因而可以在將來再使用這些匹配的元素。但除了這個元素本身得以保留之外,其他的比如繫結的事件,附加的資料等都會被移除。

$("p").remove();

$("p").remove(".remove");

5.3 detach([expr]) 從dom中刪除所有匹配的元素,這個方法不會把匹配的元素從jquery物件中刪除,因而可以在將來再使用這些匹配的元素。與remove()不同的是,所有繫結的事件、附加的資料等都會保留下來。

$("p").detach();

$("p").detach(".detach");    

6.複製

6.1 clone([even[,deepeven]]) 轉殖匹配的dom元素並且選中這些轉殖的副本。

even 乙個布林值(true 或者 false)指示事件處理函式是否會被複製。

$("b").clone().prependto("p");    //轉殖所有b元素(並選中這些轉殖的副本),然後將它們前置到所有段落中。

$("button").click(function());

jquery 文件處理

兩個引數乙個將來是父,乙個將來是子 1 appe ndto 父 1 prependto 父 將1節點插入到父容器的最後乙個節點之後 第乙個子節點之前 父 appe nd 1 父 prepend 1 實現的是和上述一樣的功能 after before兩個引數最終會是兄弟關係 1.after 2 將2插...

JQuery文件處理

p ap pend fun ctio n in dex,html ret urni ndex 這個 集合中的 索引值 htm l 這 個物件原 先的ht ml值 1.2ap pend to c onte nt 把 所有匹配 的元素追 加到另一 個指定的 元素元素 集合中。使用這個 方法是顛 倒了常規...

jQuery 文件處理

this is a paragraph.this is another paragraph.在每個 p 元素的結尾新增內容 在每個 p 元素的結尾新增內容 方法作用 向每個匹配的元素內部追加內容。引數介紹 content 要追加到目標中的內容。用法示例 html 為i come from i lov...