2014-11-25 68 views
-1

我有一个html字符串..其中的一些锚标签没有href属性(没有href属性和没有任何价值)。我试图从HTML字符串中删除这些标签找到不包含href属性的锚标记的最佳方法?

例子:

<div class="parsys ReferenceA"><div class="parbase section reference"><div style="display:inline;" class="cq-dd-paragraph"><div class="warrantyinfo warrantyinfo_3"> 
<link rel="stylesheet" href="http://test.com/media/test.css" type="text/css"> 
     <div class="box note warranty-info hidebox"> 
       <p><a name="test" data-cqpath="234492921271682679940756642751399440576;2014296"></a><b>some text</p> 
     </div> 
     </div> 
</div> 
</div> 
<div class="parbase section reference"><div style="display:inline;" class="cq-dd-paragraph"><div class="warrantyinfo"> 
<link rel="stylesheet" href="http://test.com/media/test.css" type="text/css"> 
     <div class="box note warranty-info hidebox"> 
       <p><a name="test" data-cqpath="84618115959711848855398939109764576374;2014212"></a>some text</p> 
     </div> 
     </div> 
</div> 
</div> 
<div class="parbase section reference"><div style="display:inline;" class="cq-dd-paragraph"><div class="warrantyinfo_0 warrantyinfo"> 
<link rel="stylesheet" href="http://test.com/media/test.css" type="text/css"> 
     <div class="box note warranty-info hidebox"> 
       <p><b>Bsome text<br> 
<br> 
<b>some text/p> 
     </div> 
     </div> 
</div> 
</div> 
<div class="parbase section reference"><div style="display:inline;" class="cq-dd-paragraph"><div class="warrantyinfo"> 
<link rel="stylesheet" href="http://test.com/media/test.css" type="text/css"> 
     <div class="box note warranty-info hidebox"> 
       <p>some text<br> 
</p> 
     </div> 
     </div> 
</div> 
</div> 
<div class="parbase section reference"><div style="display:inline;" class="cq-dd-paragraph"><div class="warrantyinfo"> 
<link rel="stylesheet" href="http://test.com/test,css" type="text/css"> 
     <div class="box note warranty-info hidebox"> 
       <p>some text</p> 
<p></p> 
     </div> 
     </div> 
</div> 
</div> 
</div> 

我尝试下面的代码,但没有奏效

数据是一个HTML字符串在AJAX请求返回。

var newHtml = $('<div>' + data + '</div>'); 
newHtml.find("a:not([href])").replaceWith(function() { return this.childNodes; }); 
var content = newHtml.html(); 

任何输入理解..

更新:

添加样品的HTML。

由于某些原因,空标签根本不会被替换。

http://jsfiddle.net/8WpDn/51/

如果我检查输出HTML我仍然可以看到下面的emppty标签

<p><a name="test" data-cqpath="84618115959711848855398939109764576374;2014212"></a>some text</p> 

回答

0

你的方法works

正如大卫托马斯已经指出,JQuery已经实现了你在做什么与unwrap()

newHtml.find("a:not([href])").contents().unwrap(),参见example

编辑:我看到您的编辑提到的空链接问题,但只有unwrap()!你的方法really does work! :)

+0

它的作品** only **当有文字在'' – Neverever 2014-11-25 01:28:07

相关问题