Home >php教程 >PHP源码 >jquery删除指定的html标签并保留标签内文本内容的方法

jquery删除指定的html标签并保留标签内文本内容的方法

PHP中文网
PHP中文网Original
2016-05-25 17:00:432555browse

有时我们希望删除这段html代码里面的一对e388a4556c0f65e1904146cc1a846bee标签,但是要保持里面的内容不被删除,本文提供的这个jquery方法就可以简单实现。


例如有这样一段html代码:

 代码如下:

<p>大家好,欢迎来到jb51.net</p>  
这里为大家提供最优秀的<a href="http://www.jb51.net/js">js</a>代码和教程


我们希望删除这段html代码里面的一对e388a4556c0f65e1904146cc1a846bee标签,但是要保持里面的内容不被删除,这里我们用jquery处理,详细的jQuery代码:

代码如下:

<script type="text/javascript" src=&#39;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&#39;></script>  
<script type="text/javascript">  
  $(function(){  
      $("p").each(function(){  
          var xx=$(this).html();  
          $(this).replaceWith(xx);  
      })  
  })    
</script>

这里通过jquery的html方法和replaceWith方法对p标签进行遍历替换

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn