删除顺序的< br>在CMS用户通过富文本编辑器填充的div中 [英] Remove sequential <br> in div filled by CMS users via rich text editors
问题描述
我有一个由CMS用户填充的div区域,并通过富文本编辑器发送数据.如何使用jQuery在<div>
中删除两个以上的顺序 br 标签?
I have div-area which is filled by CMS users and sends data via a rich text editor. How can I remove sequential br tags more than two in a <div>
using jQuery?
我尝试了parent()
,closest()
,但是没有用.
I tried parent()
, closest()
but it didn't work.
例如,一位CMS用户使用富文本编辑器添加了此HTML代码.
For example, a CMS user added this HTML code using a rich text editor.
<div class="cms-data">
<br>
<span>
<br><br><br><br><br><br>
<a href="/uploads/755/a1.xls" target="_blank">xls</span></a>
<br><br><br>
</span>
<br><br>
</div>
编辑:我在<div>
cms-data中添加了一个类名.
I added a class name to the <div>
, cms-data.
推荐答案
这里是一种方法:
$("#starting-point").find("br").each(function() {
if (this.previousSibling && this.previousSibling.nodeName.toUpperCase() == 'BR') {
$(this).remove();
}
});
这将找到给定容器内的所有br
元素(在我的情况下是带有id
"starting-point"
的元素).然后按文档顺序遍历它们.如果我们有一个前一个兄弟姐妹是br
元素,则将其删除.如果它前面的东西不是br
元素(例如文本节点或非br
元素),我们将不理会它.请注意,这并不认为<br> <br>
是两个相应的元素,因为它们之间有一个空格.
That finds all of the br
elements within the given container (in my case, the element with the id
"starting-point"
). Then it loops through them in document order. If we have one that has an immediate previous sibling that's a br
element, we remove it. We leave it alone if the thing just in front of it is not a br
element (such as a text node, or a non-br
element). Note that this doesn't consider <br> <br>
to be two consequtive elements, because there's a space between them.
这篇关于删除顺序的< br>在CMS用户通过富文本编辑器填充的div中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!