juqery 學(xué)習(xí)之五 文檔處理 包裹、替換、刪除、復(fù)制
更新時間:2011年02月11日 02:01:21 作者:
這個函數(shù)的原理是檢查提供的第一個元素(它是由所提供的HTML標記代碼動態(tài)生成的),并在它的代碼結(jié)構(gòu)中找到最上層的祖先元素--這個祖先元素就是包裹元素。
wrap(html)
把所有匹配的元素用其他元素的結(jié)構(gòu)化標記包裹起來。
這種包裝對于在文檔中插入額外的結(jié)構(gòu)化標記最有用,而且它不會破壞原始文檔的語義品質(zhì)。
這個函數(shù)的原理是檢查提供的第一個元素(它是由所提供的HTML標記代碼動態(tài)生成的),并在它的代碼結(jié)構(gòu)中找到最上層的祖先元素--這個祖先元素就是包裹元素。
當HTML標記代碼中的元素包含文本時無法使用這個函數(shù)。因此,如果要添加文本應(yīng)該在包裹完成之后再行添加。
--------------------------------------------------------------------------------
Wrap all matched elements with a structure of other elements.
This wrapping process is most useful for injecting additional structure into a document, without ruining the original semantic qualities of a document.
This works by going through the first element provided (which is generated, on the fly, from the provided HTML) and finds the deepest ancestor element within its structure -- it is that element that will enwrap everything else.
This does not work with elements that contain text. Any necessary text must be added after the wrapping is done.
返回值
jQuery
參數(shù)
html (String) : HTML標記代碼字符串,用于動態(tài)生成元素并包裹目標元素
示例
把所有的段落用一個新創(chuàng)建的div包裹起來
HTML 代碼:
<p>Test Paragraph.</p>
jQuery 代碼:
$("p").wrap("<div class='wrap'></div>");
結(jié)果:
<div class="wrap"><p>Test Paragraph.</p></div>
-------------------------------------------------------------------------------------------------------------------------------------------------
wrap(elem)
把所有匹配的元素用其他元素的結(jié)構(gòu)化標記包裝起來。
--------------------------------------------------------------------------------
Wrap all matched elements with a structure of other elements.
返回值
jQuery
參數(shù)
elem (Element) : 用于包裝目標元素的DOM元素
示例
用ID是"content"的div將每一個段落包裹起來
HTML 代碼:
<p>Test Paragraph.</p><div id="content"></div>
jQuery 代碼:
$("p").wrap(document.getElementById('content'));
結(jié)果:
<div id="content"><p>Test Paragraph.</p></div><div id="content"></div>
-------------------------------------------------------------------------------------------------------------------------------------------------
wrapAll(html)
將所有匹配的元素用單個元素包裹起來
這于 '.wrap()' 是不同的,'.wrap()'為每一個匹配的元素都包裹一次。
這種包裝對于在文檔中插入額外的結(jié)構(gòu)化標記最有用,而且它不會破壞原始文檔的語義品質(zhì)。
這個函數(shù)的原理是檢查提供的第一個元素并在它的代碼結(jié)構(gòu)中找到最上層的祖先元素--這個祖先元素就是包裝元素。
--------------------------------------------------------------------------------
Wrap all the elements in the matched set into a single wrapper element.
This is different from '.wrap()' where each element in the matched set would get wrapped with an element.
This wrapping process is most useful for injecting additional structure into a document, without ruining the original semantic qualities of a document.
This works by going through the first element provided (which is generated, on the fly, from the provided HTML) and finds the deepest ancestor element within its structure -- it is that element that will enwrap everything else.
返回值
jQuery
參數(shù)
html (String) : TML標記代碼字符串,用于動態(tài)生成元素并包裝目標元素
示例
用一個生成的div將所有段落包裹起來
HTML 代碼:
<p>Hello</p><p>cruel</p><p>World</p>
jQuery 代碼:
$("p").wrapAll("<div></div>");
結(jié)果:
<div><p>Hello</p><p>cruel</p><p>World</p></div>
-------------------------------------------------------------------------------------------------------------------------------------------------
wrapAll(elem)
將所有匹配的元素用單個元素包裹起來
這于 '.wrap()' 是不同的,'.wrap()'為每一個匹配的元素都包裹一次。
--------------------------------------------------------------------------------
Wrap all the elements in the matched set into a single wrapper element.
This is different from '.wrap()' where each element in the matched set would get wrapped with an element.
返回值
jQuery
參數(shù)
elem (Element) : 用于包裝目標元素的DOM元素
示例
用一個生成的div將所有段落包裹起來
HTML 代碼:
<p>Hello</p><p>cruel</p><p>World</p>
jQuery 代碼:
$("p").wrapAll(document.createElement("div"));
結(jié)果:
<div><p>Hello</p><p>cruel</p><p>World</p></div>
-------------------------------------------------------------------------------------------------------------------------------------------------
wrapInner(html)
將每一個匹配的元素的子內(nèi)容(包括文本節(jié)點)用一個HTML結(jié)構(gòu)包裹起來
這個函數(shù)的原理是檢查提供的第一個元素(它是由所提供的HTML標記代碼動態(tài)生成的),并在它的代碼結(jié)構(gòu)中找到最上層的祖先元素--這個祖先元素就是包裝元素。
--------------------------------------------------------------------------------
Wrap the inner child contents of each matched element (including text nodes) with an HTML structure.
This wrapping process is most useful for injecting additional structure into a document, without ruining the original semantic qualities of a document. This works by going through the first element provided (which is generated, on the fly, from the provided HTML) and finds the deepest ancestor element within its structure -- it is that element that will enwrap everything else.
返回值
jQuery
參數(shù)
html (String) : HTML標記代碼字符串,用于動態(tài)生成元素并包裝目標元素
示例
把所有段落內(nèi)的每個子內(nèi)容加粗
HTML 代碼:
<p>Hello</p><p>cruel</p><p>World</p>
jQuery 代碼:
$("p").wrapInner("<b></b>");
結(jié)果:
<p><b>Hello</b></p><p><b>cruel</b></p><p><b>World</b></p>
-------------------------------------------------------------------------------------------------------------------------------------------------
wrapInner(elem)
將每一個匹配的元素的子內(nèi)容(包括文本節(jié)點)用DOM元素包裹起來
--------------------------------------------------------------------------------
Wrap the inner child contents of each matched element (including text nodes) with a DOM element.
返回值
jQuery
參數(shù)
elem (Element) : 用于包裝目標元素的DOM元素
示例
把所有段落內(nèi)的每個子內(nèi)容加粗
HTML 代碼:
<p>Hello</p><p>cruel</p><p>World</p>
jQuery 代碼:
$("p").wrapInner(document.createElement("b"));
結(jié)果:
<p><b>Hello</b></p><p><b>cruel</b></p><p><b>World</b></p>
-------------------------------------------------------------------------------------------------------------------------------------------------
replaceWith(content)
將所有匹配的元素替換成指定的HTML或DOM元素。
--------------------------------------------------------------------------------
Replaces all matched elements with the specified HTML or DOM elements.
返回值
jQuery
參數(shù)
content (String, Element, jQuery) : 用于將匹配元素替換掉的內(nèi)容
示例
把所有的段落標記替換成加粗的標記。
HTML 代碼:
<p>Hello</p><p>cruel</p><p>World</p>
jQuery 代碼:
$("p").replaceWith("<b>Paragraph. </b>");
結(jié)果:
<b>Paragraph. </b><b>Paragraph. </b><b>Paragraph. </b>
-------------------------------------------------------------------------------------------------------------------------------------------------
replaceAll(selector)
用匹配的元素替換掉所有 selector匹配到的元素。
--------------------------------------------------------------------------------
Replaces the elements matched by the specified selector with the matched elements.
返回值
jQuery
參數(shù)
selector (選擇器) : 用于查找所要被替換的元素
示例
把所有的段落標記替換成加粗標記
HTML 代碼:
<p>Hello</p><p>cruel</p><p>World</p>
jQuery 代碼:
$("<b>Paragraph. </b>").replaceAll("p");
結(jié)果:
<b>Paragraph. </b><b>Paragraph. </b><b>Paragraph. </b>
-------------------------------------------------------------------------------------------------------------------------------------------------
empty()
刪除匹配的元素集合中所有的子節(jié)點。
--------------------------------------------------------------------------------
Remove all child nodes from the set of matched elements.
返回值
jQuery
示例
把所有段落的子元素(包括文本節(jié)點)刪除
HTML 代碼:
<p>Hello, <span>Person</span> <a href="#">and person</a></p>
jQuery 代碼:
$("p").empty();
結(jié)果:
<p></p>
-------------------------------------------------------------------------------------------------------------------------------------------------
remove([expr])
從DOM中刪除所有匹配的元素。
這個方法不會把匹配的元素從jQuery對象中刪除,因而可以在將來再使用這些匹配的元素。
--------------------------------------------------------------------------------
Removes all matched elements from the DOM.
This does NOT remove them from the jQuery object, allowing you to use the matched elements further. Can be filtered with an optional expression.
返回值
jQuery
參數(shù)
expr (String) : (可選) 用于篩選元素的jQuery表達式
示例
從DOM中把所有段落刪除
HTML 代碼:
<p>Hello</p> how are <p>you?</p>
jQuery 代碼:
$("p").remove();
結(jié)果:
how are
--------------------------------------------------------------------------------
從DOM中把帶有hello類的段落刪除
HTML 代碼:
<p class="hello">Hello</p> how are <p>you?</p>
jQuery 代碼:
$("p").remove(".hello");
結(jié)果:
how are <p>you?</p>
-------------------------------------------------------------------------------------------------------------------------------------------------
clone()
克隆匹配的DOM元素并且選中這些克隆的副本。
在想把DOM文檔中元素的副本添加到其他位置時這個函數(shù)非常有用。
--------------------------------------------------------------------------------
Clone matched DOM Elements and select the clones.
This is useful for moving copies of the elements to another location in the DOM.
返回值
jQuery
示例
克隆所有b元素(并選中這些克隆的副本),然后將它們前置到所有段落中。
HTML 代碼:
<b>Hello</b><p>, how are you?</p>
jQuery 代碼:
$("b").clone().prependTo("p");
結(jié)果:
<b>Hello</b><p><b>Hello</b>, how are you?</p>
-------------------------------------------------------------------------------------------------------------------------------------------------
clone(true)
元素以及其所有的事件處理并且選中這些克隆的副本
在想把DOM文檔中元素的副本添加到其他位置時這個函數(shù)非常有用。
--------------------------------------------------------------------------------
Clone matched DOM Elements, and all their event handlers, and select the clones.
This is useful for moving copies of the elements, and their events, to another location in the DOM.
返回值
jQuery
參數(shù)
true (Boolean) : 設(shè)置為true以便復(fù)制元素的所有事件處理
示例
創(chuàng)建一個按鈕,他可以復(fù)制自己,并且他的副本也有同樣功能。
HTML 代碼:
<button>Clone Me!</button>
jQuery 代碼:
$("button").click(function(){
$(this).clone(true).insertAfter(this);
});
把所有匹配的元素用其他元素的結(jié)構(gòu)化標記包裹起來。
這種包裝對于在文檔中插入額外的結(jié)構(gòu)化標記最有用,而且它不會破壞原始文檔的語義品質(zhì)。
這個函數(shù)的原理是檢查提供的第一個元素(它是由所提供的HTML標記代碼動態(tài)生成的),并在它的代碼結(jié)構(gòu)中找到最上層的祖先元素--這個祖先元素就是包裹元素。
當HTML標記代碼中的元素包含文本時無法使用這個函數(shù)。因此,如果要添加文本應(yīng)該在包裹完成之后再行添加。
--------------------------------------------------------------------------------
Wrap all matched elements with a structure of other elements.
This wrapping process is most useful for injecting additional structure into a document, without ruining the original semantic qualities of a document.
This works by going through the first element provided (which is generated, on the fly, from the provided HTML) and finds the deepest ancestor element within its structure -- it is that element that will enwrap everything else.
This does not work with elements that contain text. Any necessary text must be added after the wrapping is done.
返回值
jQuery
參數(shù)
html (String) : HTML標記代碼字符串,用于動態(tài)生成元素并包裹目標元素
示例
把所有的段落用一個新創(chuàng)建的div包裹起來
HTML 代碼:
<p>Test Paragraph.</p>
jQuery 代碼:
$("p").wrap("<div class='wrap'></div>");
結(jié)果:
<div class="wrap"><p>Test Paragraph.</p></div>
-------------------------------------------------------------------------------------------------------------------------------------------------
wrap(elem)
把所有匹配的元素用其他元素的結(jié)構(gòu)化標記包裝起來。
--------------------------------------------------------------------------------
Wrap all matched elements with a structure of other elements.
返回值
jQuery
參數(shù)
elem (Element) : 用于包裝目標元素的DOM元素
示例
用ID是"content"的div將每一個段落包裹起來
HTML 代碼:
<p>Test Paragraph.</p><div id="content"></div>
jQuery 代碼:
$("p").wrap(document.getElementById('content'));
結(jié)果:
<div id="content"><p>Test Paragraph.</p></div><div id="content"></div>
-------------------------------------------------------------------------------------------------------------------------------------------------
wrapAll(html)
將所有匹配的元素用單個元素包裹起來
這于 '.wrap()' 是不同的,'.wrap()'為每一個匹配的元素都包裹一次。
這種包裝對于在文檔中插入額外的結(jié)構(gòu)化標記最有用,而且它不會破壞原始文檔的語義品質(zhì)。
這個函數(shù)的原理是檢查提供的第一個元素并在它的代碼結(jié)構(gòu)中找到最上層的祖先元素--這個祖先元素就是包裝元素。
--------------------------------------------------------------------------------
Wrap all the elements in the matched set into a single wrapper element.
This is different from '.wrap()' where each element in the matched set would get wrapped with an element.
This wrapping process is most useful for injecting additional structure into a document, without ruining the original semantic qualities of a document.
This works by going through the first element provided (which is generated, on the fly, from the provided HTML) and finds the deepest ancestor element within its structure -- it is that element that will enwrap everything else.
返回值
jQuery
參數(shù)
html (String) : TML標記代碼字符串,用于動態(tài)生成元素并包裝目標元素
示例
用一個生成的div將所有段落包裹起來
HTML 代碼:
<p>Hello</p><p>cruel</p><p>World</p>
jQuery 代碼:
$("p").wrapAll("<div></div>");
結(jié)果:
<div><p>Hello</p><p>cruel</p><p>World</p></div>
-------------------------------------------------------------------------------------------------------------------------------------------------
wrapAll(elem)
將所有匹配的元素用單個元素包裹起來
這于 '.wrap()' 是不同的,'.wrap()'為每一個匹配的元素都包裹一次。
--------------------------------------------------------------------------------
Wrap all the elements in the matched set into a single wrapper element.
This is different from '.wrap()' where each element in the matched set would get wrapped with an element.
返回值
jQuery
參數(shù)
elem (Element) : 用于包裝目標元素的DOM元素
示例
用一個生成的div將所有段落包裹起來
HTML 代碼:
<p>Hello</p><p>cruel</p><p>World</p>
jQuery 代碼:
$("p").wrapAll(document.createElement("div"));
結(jié)果:
<div><p>Hello</p><p>cruel</p><p>World</p></div>
-------------------------------------------------------------------------------------------------------------------------------------------------
wrapInner(html)
將每一個匹配的元素的子內(nèi)容(包括文本節(jié)點)用一個HTML結(jié)構(gòu)包裹起來
這個函數(shù)的原理是檢查提供的第一個元素(它是由所提供的HTML標記代碼動態(tài)生成的),并在它的代碼結(jié)構(gòu)中找到最上層的祖先元素--這個祖先元素就是包裝元素。
--------------------------------------------------------------------------------
Wrap the inner child contents of each matched element (including text nodes) with an HTML structure.
This wrapping process is most useful for injecting additional structure into a document, without ruining the original semantic qualities of a document. This works by going through the first element provided (which is generated, on the fly, from the provided HTML) and finds the deepest ancestor element within its structure -- it is that element that will enwrap everything else.
返回值
jQuery
參數(shù)
html (String) : HTML標記代碼字符串,用于動態(tài)生成元素并包裝目標元素
示例
把所有段落內(nèi)的每個子內(nèi)容加粗
HTML 代碼:
<p>Hello</p><p>cruel</p><p>World</p>
jQuery 代碼:
$("p").wrapInner("<b></b>");
結(jié)果:
<p><b>Hello</b></p><p><b>cruel</b></p><p><b>World</b></p>
-------------------------------------------------------------------------------------------------------------------------------------------------
wrapInner(elem)
將每一個匹配的元素的子內(nèi)容(包括文本節(jié)點)用DOM元素包裹起來
--------------------------------------------------------------------------------
Wrap the inner child contents of each matched element (including text nodes) with a DOM element.
返回值
jQuery
參數(shù)
elem (Element) : 用于包裝目標元素的DOM元素
示例
把所有段落內(nèi)的每個子內(nèi)容加粗
HTML 代碼:
<p>Hello</p><p>cruel</p><p>World</p>
jQuery 代碼:
$("p").wrapInner(document.createElement("b"));
結(jié)果:
<p><b>Hello</b></p><p><b>cruel</b></p><p><b>World</b></p>
-------------------------------------------------------------------------------------------------------------------------------------------------
replaceWith(content)
將所有匹配的元素替換成指定的HTML或DOM元素。
--------------------------------------------------------------------------------
Replaces all matched elements with the specified HTML or DOM elements.
返回值
jQuery
參數(shù)
content (String, Element, jQuery) : 用于將匹配元素替換掉的內(nèi)容
示例
把所有的段落標記替換成加粗的標記。
HTML 代碼:
<p>Hello</p><p>cruel</p><p>World</p>
jQuery 代碼:
$("p").replaceWith("<b>Paragraph. </b>");
結(jié)果:
<b>Paragraph. </b><b>Paragraph. </b><b>Paragraph. </b>
-------------------------------------------------------------------------------------------------------------------------------------------------
replaceAll(selector)
用匹配的元素替換掉所有 selector匹配到的元素。
--------------------------------------------------------------------------------
Replaces the elements matched by the specified selector with the matched elements.
返回值
jQuery
參數(shù)
selector (選擇器) : 用于查找所要被替換的元素
示例
把所有的段落標記替換成加粗標記
HTML 代碼:
<p>Hello</p><p>cruel</p><p>World</p>
jQuery 代碼:
$("<b>Paragraph. </b>").replaceAll("p");
結(jié)果:
<b>Paragraph. </b><b>Paragraph. </b><b>Paragraph. </b>
-------------------------------------------------------------------------------------------------------------------------------------------------
empty()
刪除匹配的元素集合中所有的子節(jié)點。
--------------------------------------------------------------------------------
Remove all child nodes from the set of matched elements.
返回值
jQuery
示例
把所有段落的子元素(包括文本節(jié)點)刪除
HTML 代碼:
<p>Hello, <span>Person</span> <a href="#">and person</a></p>
jQuery 代碼:
$("p").empty();
結(jié)果:
<p></p>
-------------------------------------------------------------------------------------------------------------------------------------------------
remove([expr])
從DOM中刪除所有匹配的元素。
這個方法不會把匹配的元素從jQuery對象中刪除,因而可以在將來再使用這些匹配的元素。
--------------------------------------------------------------------------------
Removes all matched elements from the DOM.
This does NOT remove them from the jQuery object, allowing you to use the matched elements further. Can be filtered with an optional expression.
返回值
jQuery
參數(shù)
expr (String) : (可選) 用于篩選元素的jQuery表達式
示例
從DOM中把所有段落刪除
HTML 代碼:
<p>Hello</p> how are <p>you?</p>
jQuery 代碼:
$("p").remove();
結(jié)果:
how are
--------------------------------------------------------------------------------
從DOM中把帶有hello類的段落刪除
HTML 代碼:
<p class="hello">Hello</p> how are <p>you?</p>
jQuery 代碼:
$("p").remove(".hello");
結(jié)果:
how are <p>you?</p>
-------------------------------------------------------------------------------------------------------------------------------------------------
clone()
克隆匹配的DOM元素并且選中這些克隆的副本。
在想把DOM文檔中元素的副本添加到其他位置時這個函數(shù)非常有用。
--------------------------------------------------------------------------------
Clone matched DOM Elements and select the clones.
This is useful for moving copies of the elements to another location in the DOM.
返回值
jQuery
示例
克隆所有b元素(并選中這些克隆的副本),然后將它們前置到所有段落中。
HTML 代碼:
<b>Hello</b><p>, how are you?</p>
jQuery 代碼:
$("b").clone().prependTo("p");
結(jié)果:
<b>Hello</b><p><b>Hello</b>, how are you?</p>
-------------------------------------------------------------------------------------------------------------------------------------------------
clone(true)
元素以及其所有的事件處理并且選中這些克隆的副本
在想把DOM文檔中元素的副本添加到其他位置時這個函數(shù)非常有用。
--------------------------------------------------------------------------------
Clone matched DOM Elements, and all their event handlers, and select the clones.
This is useful for moving copies of the elements, and their events, to another location in the DOM.
返回值
jQuery
參數(shù)
true (Boolean) : 設(shè)置為true以便復(fù)制元素的所有事件處理
示例
創(chuàng)建一個按鈕,他可以復(fù)制自己,并且他的副本也有同樣功能。
HTML 代碼:
<button>Clone Me!</button>
jQuery 代碼:
$("button").click(function(){
$(this).clone(true).insertAfter(this);
});
相關(guān)文章
jquery pagination分頁插件使用詳解(后臺struts2)
這篇文章主要為大家詳細介紹了jquery pagination 分頁插件的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01基于jQuery插件實現(xiàn)環(huán)形圖標菜單旋轉(zhuǎn)切換特效
本文給大家分享一款基于jQuery環(huán)形圖標旋轉(zhuǎn)切換特效。這是一款鼠標點擊圖標菜單圓形順時針或者逆時針旋轉(zhuǎn)切換代碼。有需要的小伙伴可以參考下。2015-05-05分享20款美化網(wǎng)站的 jQuery Lightbox 燈箱插件
這篇文章主要介紹了分享20款美化網(wǎng)站的 jQuery Lightbox 燈箱插件,需要的朋友可以參考下2014-10-10jQuery實現(xiàn)驗證表單密碼一致性及正則表達式驗證郵箱、手機號的方法
這篇文章主要介紹了jQuery實現(xiàn)驗證表單密碼一致性及正則表達式驗證郵箱、手機號的方法,涉及jQuery表單元素獲取及正則驗證相關(guān)操作技巧,需要的朋友可以參考下2017-12-12jquery插件uploadify多圖上傳功能實現(xiàn)代碼
這篇文章主要為大家詳細介紹了jquery插件uploadify多圖上傳功能實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08