日期:2014-05-16  浏览次数:20319 次

Jquery如何删除P标签
请问Jquery如何删除P标签?
数据是有数据库读出来的,内含P标签!显示后就有段落的效果!但我不想要P标签有效果,想利用Jquery删除P标签!但我的功力不够,试一试却把P标签内容清空了!
所有的P标签都是这样写的!

<p>内容</p>

------解决方案--------------------
$("p").remove();
------解决方案--------------------
HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
</head>

<body>
<div>
    <p>test1</p>
    <p>test2</p>
</div>
<script type="text/javascript">
var txt = $("div p").text();
$("div").html(txt);
</script>
</body>
</html>

------解决方案--------------------
replaceWith(content)将所有匹配的元素替换成指定的HTML或DOM元素。


HTML code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
  <style type="text/css">
   
  </style>
  <script type="text/javascript" src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
  <script type="text/javascript">
    $(function(){
        $("p").each(function(){
            var xx=$(this).html();
            $(this).replaceWith(xx);
        })
    })  
  </script>
 </HEAD>

 <BODY>
    <p>你在他乡还好吗</p><p>灭烛怜光满!!</p>
</BODY>
</HTML>

------解决方案--------------------
$("div:has(p)").remove();
//or
$(obj).is("p")..