经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » jQuery » 查看文章
jQuery 源码解析(二十三) DOM操作模块 替换元素 详解
来源:cnblogs  作者:大沙漠  时间:2019/11/13 9:43:18  对本文有异议

本节说一下DOM操作模块里的替换元素模块,该模块可将当前匹配的元素替换指定的DOM元素,有两个方法,如下:

  • replaceWith(value)     ;使用提供的新内容来替换匹配元素集合中的每个元素。value是新内容,可以是html字符串、DOM元素、jQuery对象或返回新内容的函数。
  • replaceAll(value)        ;使用匹配元素集合中的元素替换目标元素。内部执行.replaceWith(value)方法,只是语法顺序上不同。类似于append()和appendTo()。

举个栗子:

 writer by:大沙漠 QQ:22969969

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <script src="http://libs.baidu.com/jquery/1.7.1/jquery.min.js"></script>
  7. </head>
  8. <body>
  9. <div id="test"><i>Hello World!</i></div>
  10. <button id="b1">测试1</button>
  11. <button id="b2">测试2</button>
  12. <script>
  13. b1.onclick=function(){ //修改#test的内容,用replaceWith()方法
  14. $('#test').replaceWith('<h1 id="test">Hello jQuery!</h1>')
  15. }
  16. b2.onclick=function(){ //修改#test的内容,用replaceAll()方法
  17. $('<p id="test">jQuery is Good!</p>').replaceAll('#test')
  18. }
  19. </script>
  20. </body>
  21. </html>

渲染如下:

当点击测试1按钮时,页面渲染如下:

当点击测试2按钮时,页面变为如下:

 

 源码分析


 replaceWith()的实现比较简单,代码如下:

  1. jQuery.fn.extend({
  2. replaceWith: function( value ) { //使用提供的新内容来替换匹配元素集合中的每个元素。value是新内容,可以是html字符串、DOM元素、jQuery对象或返回新内容的函数。
  3. if ( this[0] && this[0].parentNode ) { //如果至少有一个匹配元素,且该元素有父元素
  4. // Make sure that the elements are removed from the DOM before they are inserted
  5. // this can help fix replacing a parent with child elements
  6. if ( jQuery.isFunction( value ) ) { //如果value是函数
  7. return this.each(function(i) { //遍历匹配元素集合
  8. var self = jQuery(this), old = self.html();
  9. self.replaceWith( value.call( this, i, old ) ); //给每个元素调用value函数,并用该函数的返回值迭代调用replaceWith()函数。
  10. });
  11. }
  12. if ( typeof value !== "string" ) { //如果参数value不是字符串,则可能是DOM元素或jQuery对象
  13. value = jQuery( value ).detach(); //先用value创建一个jQuery对象,再调用.detach()把参数从value文档中删除,保留关联的数据和事件。
  14. }
  15. return this.each(function() { //遍历当前匹配元素
  16. var next = this.nextSibling, //下一个元素节点引用
  17. parent = this.parentNode; //上一个元素节点引用
  18. jQuery( this ).remove(); //调用.remove()移除后代元素和当前元素关联的数据和事件,以避免内存泄漏。
  19.  
  20. if ( next ) { //如果有下一个元素
  21. jQuery(next).before( value ); //则调用$.fn.before()函数将新内容插入下一个兄弟元素之前;
  22. } else {
  23. jQuery(parent).append( value ); //否则调用$.fn.append()函数则将新内容插入父元素末尾。
  24. }
  25. });
  26. } else {
  27. return this.length ?
  28. this.pushStack( jQuery(jQuery.isFunction(value) ? value() : value), "replaceWith", value ) :
  29. this;
  30. }
  31. },
  32. })

比较简单,就是先调用remove()移除当前之前,然后调用前一个节点的before()或父节点的append()方法插入新的节点即可,对于replaceAll()来说,它和插入元素那几个方法一样,是replaceWith()的另一个写法,可以看这个链接:https://www.cnblogs.com/greatdesert/p/11732436.html

原文链接:http://www.cnblogs.com/greatdesert/p/11819325.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号