Jquery 找到不包含href属性的锚定标记的最佳方法?
我有一个html字符串。。其中的一些锚定标记没有href属性(没有href属性,也没有任何值)。我正在尝试从HTML字符串中删除这些标记 例如:Jquery 找到不包含href属性的锚定标记的最佳方法?,jquery,anchor,Jquery,Anchor,我有一个html字符串。。其中的一些锚定标记没有href属性(没有href属性,也没有任何值)。我正在尝试从HTML字符串中删除这些标记 例如: <div class="parsys ReferenceA"><div class="parbase section reference"><div style="display:inline;" class="cq-dd-paragraph"><div class="warrantyinfo warranty
<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>
一些文字
b一些文本
一些文本/p>
一些文本
一些文字
我尝试了下面的代码,但没有成功
数据是根据ajax请求返回的html字符串
var newHtml = $('<div>' + data + '</div>');
newHtml.find("a:not([href])").replaceWith(function() { return this.childNodes; });
var content = newHtml.html();
var newHtml=$(''+数据+'');
newHtml.find(“a:not([href])”).replaceWith(函数(){return this.childNodes;});
var content=newHtml.html();
欢迎您的任何意见
更新:
添加了示例html
出于某种原因,空标签根本不会被替换
如果我检查输出html,我仍然可以看到下面的empty标记
<p><a name="test" data-cqpath="84618115959711848855398939109764576374;2014212"></a>some text</p>
一些文本
试试看
你的方法
正如DavidThomas所指出的,JQuery已经实现了您正在使用的功能
newHtml.find(“a:not([href])”).contents().unwrap()
,请参阅
编辑:我看到了编辑中提到的空链接问题,但仅限于unwrap()!你的方法!:) 我想不出更简单的方法来查找
。你能举一个简单的例子说明数据中的变量吗?因为这看起来应该有效,但显然“没有”,这是对您遇到的问题的非常模糊的描述。你能再仔细一点吗?你发布的代码很有效。顺便说一句,历史上使用
导航到页面的一部分。在HTML5中,这是通过id完成的。HTML5中的标记没有名称属性,因此您可能想用
替换
,OP需要描述不起作用的内容
,它是否替换了错误的内容或根本没有替换它看起来提供的示例代码试图保留子节点。此解决方案将同时删除
和子节点,对吗?从示例HTML中,在
中没有子节点。您是对的。这表明这个问题可能需要一些工作。我认为.remove
仍然是最好的方法。它只有在
之间有文本时才起作用
newHtml.find("a:not([href])").remove();