admin管理员组文章数量:1431488
So I am receiving unclean content from an API that our content publishers are using and I am getting P tags with non-breaking spaces inside of them and I want to remove the non-breaking space and the P it is in. It would be nice if they cleaned up their content before they published it to the API, but I just need a quick way to remove this via JavaScript or jQuery. I get this error when using the code below. "Syntax error, unrecognized expression: " Thanks for any help.
<p> </p>
$("p").each(function() {
$(this).find(' ').remove();
});
So I am receiving unclean content from an API that our content publishers are using and I am getting P tags with non-breaking spaces inside of them and I want to remove the non-breaking space and the P it is in. It would be nice if they cleaned up their content before they published it to the API, but I just need a quick way to remove this via JavaScript or jQuery. I get this error when using the code below. "Syntax error, unrecognized expression: " Thanks for any help.
<p> </p>
$("p").each(function() {
$(this).find(' ').remove();
});
Share
Improve this question
asked Feb 12, 2015 at 22:18
JustinJustin
5863 gold badges10 silver badges31 bronze badges
1
- related: stackoverflow./questions/6813227/… – Scott Simpson Commented Feb 12, 2015 at 22:23
3 Answers
Reset to default 6Select your paragraph elements, filter out the ones that contain only
using .filter()
, then use .remove()
:
$('p').filter(function(){
return this.innerHTML == ' ';
}).remove();
JSFiddle
Or:
$('p').filter(function(){
return !$.trim($(this).text());
}).remove();
Which, because .text()
handles HTML decoding for you and $.trim()
removes outer whitespace, will remove any paragraph that contains only spaces I.E
<p> </p>
$(p).each(function(){
$(this).html($(this).html().replace(" ", ""));
$(this).replaceTagName('');
// or $(this).contents().unwrap(); much faster
});
This works:
$('p').filter(function () {
return $(this).html().replace(/ /g, '').length === 0
}).remove()
jsFiddle example
While you said "I want to remove the non-breaking space and the P it is in", you only need to remove the P since that will take the
with it.
本文标签: Remove P tag with nonbreaking space inside of it via JavaScript or jQueryStack Overflow
版权声明:本文标题:Remove P tag with non-breaking space inside of it via JavaScript or jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745582879a2664724.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论