Javascript 如何删除<;br>;从文本节点并使用<;p>;当其包含时标记<;b>&书信电报;i>&书信电报;a>;,等
我的状况(编辑):Javascript 如何删除<;br>;从文本节点并使用<;p>;当其包含时标记<;b>&书信电报;i>&书信电报;a>;,等,javascript,jquery,html,tags,Javascript,Jquery,Html,Tags,我的状况(编辑): 不包括此标题 作者:我 日期:2017年1月1日 当你引用“谁奋斗,谁就只为自己的利益而奋斗。” 还有一句话:“除了他为之奋斗的[善]之外,人类没有别的东西,他的努力将被看到,然后得到最充分的回报。” 这个也不会 我为自己而战。 我的目标(编辑): 不包括此标题 作者:我 日期:2017年1月1日 当你引用“谁奋斗,谁就只为自己的利益而奋斗。” 还有另一种说法:“除了人类为之奋斗的[善]之外,人类没有别的东西,他的努力将会被看到,那么,。但是他们无法处理
不包括此标题
作者:我
日期:2017年1月1日
当你引用“谁奋斗,谁就只为自己的利益而奋斗。”
还有一句话:“除了他为之奋斗的[善]之外,人类没有别的东西,他的努力将被看到,然后得到最充分的回报。”
这个也不会
我为自己而战。
我的目标(编辑):
不包括此标题
作者:我
日期:2017年1月1日
当你引用“谁奋斗,谁就只为自己的利益而奋斗。”
还有另一种说法:“除了人类为之奋斗的[善]之外,人类没有别的东西,他的努力将会被看到,那么,。但是他们无法处理
,
,
,等等
它们就像这样:
<div class="entry-content">
<p><h1>This header won't be included</h1></p>
<p>Write by: Me</p>
<p>On: 01/01/2017</p>
<p>When you quote: "</p>
<i>And whoever strives only strives for [the benefit of] himself.</i>
<p>"</p>
<i>There</i>
<p>is another: "And that <b>there is not for man</b> except that [good] for which he strives, and that his effort is going to be seen, then</p>
<a hre="#">he will be</a>
<p>recompensed for it with the fullest recompense."</p>
<p><h2>And neither will this one</h2></p>
<p>I fight for myself.</p>
</div>
不包括此标题
作者:我
日期:2017年1月1日
当你引用:
而任何奋斗的人只为自己的利益而奋斗。
"
那里
另一种说法是:“除了他为之奋斗的[善]之外,人类没有别的东西,他的努力将被看到,那么
他会的
得到了最充分的回报。”
这个也不会
我为自己而战
我希望这就是您需要的:
var rows=$(“.entry content”).html().split(“
”);
行=$.map(行,函数(值){
返回值==“”?空:值;
});
$(“.entry content”).html(“”+rows.join(“\n”)+“”);
console.log($(“.entry content”).html();
当你引用“谁奋斗,谁就只为自己的利益而奋斗。”
还有一句话:“除了他为之奋斗的[善]之外,再没有别的东西了,而且他的努力将被看到,那么他将得到最充分的回报。”
我为自己而战。
这里有一个“一行”不涉及内部HTML文本。它的工作原理是用自己的段落包装每个节点,然后展开每个br
,这样相邻的同级和第一级子级选择器就可以找到包装段落的起始点
编辑:为了特别排除包含具有源属性的图像的标题、块引号和锚定,我不得不将其延长。更有效的实现是在没有jQuery的情况下在子节点上循环;如果需要经常运行,请告诉我,我可以提供一个替代实现。它可以要解决jQuery处理文本节点的方式很困难,除了.contents()
,在每个操作中几乎都会丢弃它们
console.log(
//开始变魔术
$(“.entry内容”)
.contents()
.wrap(“”)
.filter('br,:header,blockquote,a:has(img[src]))
.unwrap()
(完)
(完)
.children(':first children,:not(p)+p')
.每个功能(即,e){
$(e).nextUntil(':not(p)').addBack().wrapAll('').contents().unwrap()
})
(完)
.children(':first children,:not(p)+p')
.每个功能(即,e){
e、 firstElementChild | | e.textContent.trim()| |$(e).remove()
})
(完)
.儿童('br')
.删除()
//结束魔法
.end().html())
不包括此标题
当你引用“谁奋斗,谁就只为自己的利益而奋斗。”
还有一句话:“除了他为之奋斗的[善]之外,人类没有别的东西,他的努力将被看到,然后得到最充分的回报。”
这个也不会
我为自己而战。
留着我
我很特别。
没有什么比什么都特别
既然两者呈现的内容完全相同,为什么要这样做呢?您的答案非常完美。非常感谢。遗憾的是,这个答案也改变了我frontpage中的内容。frontpage中的所有内容都变得相同(标题没有问题).当我点击时,也没问题,每个标题都有自己的内容,.@FuturesMe,根据你提供的代码,这是一个绝对正确的答案。根据你的答案,那么标题
,
等怎么能不用
标签包装?我以前忘了问。修改了我的答案以排除标题。这个bloa不过,我对代码进行了显著的修改,因此之前存在的任何性能都受到了很大的影响。很抱歉,我尝试了您的答案,并对
进行了修复。只需添加.filter('br,:header')即可
。最后,如何处理
以避免在代码中被包装。我希望这是关于这个问题的最后一个问题。当然。等一下,我更新代码。所以你想排除块引号,然后锚定,但前提是它们包含带有src=#
的图像,或者src可以是任何东西,元素仍然应该是Ignored?是的。我只想排除有链接的图像。
<div class="entry-content">
<h1>This header won't be included</h1>
<p>Write by: Me
<br></br>
On: 01/01/2017</p>
<p>When you quote: "<i>And whoever strives only strives for [the benefit of] himself.</i>"</p>
</p><i>There</i> is another: "And that <b>there is not for man</b> except that [good] for which he strives, and that his effort is going to be seen, then <a hre="#">he will be</a> recompensed for it with the fullest recompense."</p>
<h2>And neither will this one</h2>
<p>I fight for myself.</p>
</div>
$( ".entry-content" )
.contents()
.filter(function() {
return this.nodeType === 3;
})
.wrap( "<p></p>" )
.end()
.filter( "br" )
.remove();
<div class="entry-content">
<p><h1>This header won't be included</h1></p>
<p>Write by: Me</p>
<p>On: 01/01/2017</p>
<p>When you quote: "</p>
<i>And whoever strives only strives for [the benefit of] himself.</i>
<p>"</p>
<i>There</i>
<p>is another: "And that <b>there is not for man</b> except that [good] for which he strives, and that his effort is going to be seen, then</p>
<a hre="#">he will be</a>
<p>recompensed for it with the fullest recompense."</p>
<p><h2>And neither will this one</h2></p>
<p>I fight for myself.</p>
</div>