juqery 学习之五 文档处理 包裹、替换、删除、复制


Posted in Javascript onFebruary 11, 2011

wrap(html)
把所有匹配的元素用其他元素的结构化标记包裹起来。
这种包装对于在文档中插入额外的结构化标记最有用,而且它不会破坏原始文档的语义品质。
这个函数的原理是检查提供的第一个元素(它是由所提供的HTML标记代码动态生成的),并在它的代码结构中找到最上层的祖先元素--这个祖先元素就是包裹元素。

当HTML标记代码中的元素包含文本时无法使用这个函数。因此,如果要添加文本应该在包裹完成之后再行添加。

--------------------------------------------------------------------------------

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

参数
html (String) : HTML标记代码字符串,用于动态生成元素并包裹目标元素

示例
把所有的段落用一个新创建的div包裹起来

HTML 代码:

<p>Test Paragraph.</p>
jQuery 代码:

$("p").wrap("<div class='wrap'></div>");
结果:

<div class="wrap"><p>Test Paragraph.</p></div>
-------------------------------------------------------------------------------------------------------------------------------------------------
wrap(elem)
把所有匹配的元素用其他元素的结构化标记包装起来。

--------------------------------------------------------------------------------

Wrap all matched elements with a structure of other elements.
返回值
jQuery

参数
elem (Element) : 用于包装目标元素的DOM元素

示例
用ID是"content"的div将每一个段落包裹起来

HTML 代码:

<p>Test Paragraph.</p><div id="content"></div>
jQuery 代码:

$("p").wrap(document.getElementById('content'));
结果:

<div id="content"><p>Test Paragraph.</p></div><div id="content"></div>
-------------------------------------------------------------------------------------------------------------------------------------------------
wrapAll(html)
将所有匹配的元素用单个元素包裹起来
这于 '.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.
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

参数
html (String) : TML标记代码字符串,用于动态生成元素并包装目标元素

示例
用一个生成的div将所有段落包裹起来

HTML 代码:

<p>Hello</p><p>cruel</p><p>World</p>
jQuery 代码:

$("p").wrapAll("<div></div>");
结果:

<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

参数
elem (Element) : 用于包装目标元素的DOM元素

示例
用一个生成的div将所有段落包裹起来

HTML 代码:

<p>Hello</p><p>cruel</p><p>World</p>
jQuery 代码:

$("p").wrapAll(document.createElement("div"));
结果:

<div><p>Hello</p><p>cruel</p><p>World</p></div>
-------------------------------------------------------------------------------------------------------------------------------------------------
wrapInner(html)
将每一个匹配的元素的子内容(包括文本节点)用一个HTML结构包裹起来
这个函数的原理是检查提供的第一个元素(它是由所提供的HTML标记代码动态生成的),并在它的代码结构中找到最上层的祖先元素--这个祖先元素就是包装元素。

--------------------------------------------------------------------------------

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

参数
html (String) : HTML标记代码字符串,用于动态生成元素并包装目标元素

示例
把所有段落内的每个子内容加粗

HTML 代码:

<p>Hello</p><p>cruel</p><p>World</p>
jQuery 代码:

$("p").wrapInner("<b></b>");
结果:

<p><b>Hello</b></p><p><b>cruel</b></p><p><b>World</b></p>
-------------------------------------------------------------------------------------------------------------------------------------------------
wrapInner(elem)
将每一个匹配的元素的子内容(包括文本节点)用DOM元素包裹起来

--------------------------------------------------------------------------------

Wrap the inner child contents of each matched element (including text nodes) with a DOM element.
返回值
jQuery

参数
elem (Element) : 用于包装目标元素的DOM元素

示例
把所有段落内的每个子内容加粗

HTML 代码:

<p>Hello</p><p>cruel</p><p>World</p>
jQuery 代码:

$("p").wrapInner(document.createElement("b"));
结果:

<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

参数
content (String, Element, jQuery) : 用于将匹配元素替换掉的内容

示例
把所有的段落标记替换成加粗的标记。

HTML 代码:

<p>Hello</p><p>cruel</p><p>World</p>
jQuery 代码:

$("p").replaceWith("<b>Paragraph. </b>");
结果:

<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

参数
selector (选择器) : 用于查找所要被替换的元素

示例
把所有的段落标记替换成加粗标记

HTML 代码:

<p>Hello</p><p>cruel</p><p>World</p>
jQuery 代码:

$("<b>Paragraph. </b>").replaceAll("p");
结果:

<b>Paragraph. </b><b>Paragraph. </b><b>Paragraph. </b>
-------------------------------------------------------------------------------------------------------------------------------------------------
empty()
删除匹配的元素集合中所有的子节点。

--------------------------------------------------------------------------------

Remove all child nodes from the set of matched elements.
返回值
jQuery

示例
把所有段落的子元素(包括文本节点)删除

HTML 代码:

<p>Hello, <span>Person</span> <a href="#">and person</a></p>
jQuery 代码:

$("p").empty();
结果:

<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

参数
expr (String) : (可选) 用于筛选元素的jQuery表达式

示例
从DOM中把所有段落删除

HTML 代码:

<p>Hello</p> how are <p>you?</p>
jQuery 代码:

$("p").remove();
结果:

how are

--------------------------------------------------------------------------------

从DOM中把带有hello类的段落删除

HTML 代码:

<p class="hello">Hello</p> how are <p>you?</p>
jQuery 代码:

$("p").remove(".hello");
结果:

how are <p>you?</p>
-------------------------------------------------------------------------------------------------------------------------------------------------
clone()
克隆匹配的DOM元素并且选中这些克隆的副本。
在想把DOM文档中元素的副本添加到其他位置时这个函数非常有用。

--------------------------------------------------------------------------------

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");
结果:

<b>Hello</b><p><b>Hello</b>, how are you?</p>
-------------------------------------------------------------------------------------------------------------------------------------------------
clone(true)
元素以及其所有的事件处理并且选中这些克隆的副本
在想把DOM文档中元素的副本添加到其他位置时这个函数非常有用。

--------------------------------------------------------------------------------

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

参数
true (Boolean) : 设置为true以便复制元素的所有事件处理

示例
创建一个按钮,他可以复制自己,并且他的副本也有同样功能。

HTML 代码:

<button>Clone Me!</button>
jQuery 代码:

$("button").click(function(){
$(this).clone(true).insertAfter(this);
});

Javascript 相关文章推荐
打开超链需要“确认”对话框的方法
Mar 08 Javascript
收藏Javascript中常用的55个经典技巧
Aug 12 Javascript
基于jQuery的烟花效果(运动相关)点击屏幕出烟花
Jun 14 Javascript
javascript字符串与数组转换汇总
May 26 Javascript
IE6兼容透明背景图片及解决方案
Aug 19 Javascript
jquery实现点击其他区域时隐藏下拉div和遮罩层的方法
Dec 23 Javascript
详解jquery easyui之datagrid使用参考
Dec 05 Javascript
使用clipboard.js实现复制功能的示例代码
Oct 16 Javascript
vue-cli + sass 的正确打开方式图文详解
Oct 27 Javascript
使用VUE+iView+.Net Core上传图片的方法示例
Jan 04 Javascript
微信小程序实现发送模板消息功能示例【通过openid推送消息给用户】
May 05 Javascript
小程序自定义导航栏兼容适配所有机型(附完整案例)
Apr 26 Javascript
juqery 学习之五 文档处理 插入
Feb 11 #Javascript
基于JQuery的浮动DIV显示提示信息并自动隐藏
Feb 11 #Javascript
Javascript面向对象之四 继承
Feb 08 #Javascript
javascript面向对象之二 命名空间
Feb 08 #Javascript
javascript中的对象创建 实例附注释
Feb 08 #Javascript
kmock javascript 单元测试代码
Feb 06 #Javascript
一次失败的jQuery优化尝试小结
Feb 06 #Javascript
You might like
PHP 采集程序 常用函数
2008/12/18 PHP
php echo, print, print_r, sprintf, var_dump, var_expor的使用区别
2013/06/20 PHP
简单的pgsql pdo php操作类实现代码
2016/08/25 PHP
PHP静态成员变量
2017/02/14 PHP
三个思路解决laravel上传文件报错:413 Request Entity Too Large问题
2017/11/13 PHP
PHP实现基于图的深度优先遍历输出1,2,3...n的全排列功能
2017/11/10 PHP
PHP基于PDO扩展操作mysql数据库示例
2018/12/24 PHP
javascript编程起步(第六课)
2007/02/27 Javascript
JavaScript Eval 函数使用
2010/03/23 Javascript
一个分享按钮的插件使用介绍(可扩展,内附开发制作流程)
2011/09/19 Javascript
javascript动态判断html元素并执行不同的操作
2014/06/16 Javascript
AngularJS自动表单验证
2016/02/01 Javascript
ionic环境配置及问题详解
2017/06/27 Javascript
详解React-Native解决键盘遮挡问题(Keyboard遮挡问题)
2017/07/13 Javascript
微信小程序之GET请求的实例详解
2017/09/29 Javascript
vue.js中$set与数组更新方法
2018/03/08 Javascript
JS逻辑运算符短路操作实例分析
2018/07/09 Javascript
vuejs中监听窗口关闭和窗口刷新事件的方法
2018/09/21 Javascript
深入解析ES6中的promise
2018/11/08 Javascript
antd Upload 文件上传的示例代码
2018/12/14 Javascript
node全局变量__dirname与__filename的区别
2019/01/14 Javascript
vue基本使用--refs获取组件或元素的实例
2019/11/07 Javascript
vue element-ui读取pdf文件的方法
2019/11/26 Javascript
使用Vue-scroller页面input框不能触发滑动的问题及解决方法
2020/08/08 Javascript
[46:44]DOTA2-DPC中国联赛 正赛 Ehome vs PSG.LGD BO3 第二场 3月7日
2021/03/11 DOTA
在Python中使用__slots__方法的详细教程
2015/04/28 Python
Python中eval带来的潜在风险代码分析
2017/12/11 Python
详解Appium+Python之生成html测试报告
2019/01/04 Python
利用python实现冒泡排序算法实例代码
2019/12/01 Python
python如何实现不用装饰器实现登陆器小程序
2019/12/14 Python
瑞士首家网上药店折扣店:McDrogerie
2020/12/22 全球购物
电子专业求职信
2014/06/19 职场文书
授权委托书公证
2014/09/14 职场文书
商场广播稿范文
2015/08/19 职场文书
李白经典诗之一:全文无一“月”字,却句句有月
2019/07/12 职场文书
详解Go语言运用广度优先搜索走迷宫
2021/06/23 Python