您好,我一直在编写一个包含 RSS 提要列表的应用程序。我一直将我的按钮连接到它们的对象对应部分(显示特定提要的按钮,例如 BBC 按钮显示 BBC rss 提要,卫报显示 Guarian 新闻并隐藏 BBC),但是看着容器,由于 Handlebars 助手,它们变得相当倾斜正在合并以使提要看起来不错。
该助手允许我缩短提要描述并以省略号结束缩短的版本。这导致问题的原因是,在描述中的 maxLength HTML 标记仍然添加到页面后,其中一个提要的描述含义中包含 HTML。这使得我的容器具有额外不需要的 HTML 元素。
我希望这已经足够解释了,TL;DR 是 RSS 描述中返回的 HTML,正在向我的页面添加其他不需要的 HTML。怎么修?
我的辅助方法:
handlebars.registerHelper("rssDesc", function(results) {
var maxLength = 164;
//this checks for multiple descriptions and shows first
if(Array.isArray(results)) {
results = results[0];
}
//this checks to see if text contains html markup and converts it to text HOWEVER doesn't work yet. T_T
if(results.indexOf("<") > -1) {
results = $(results).text();
}
results = results.substring(0, maxLength);
return results.substring(0, Math.min(results.length, results.lastIndexOf(" "))) + " ...";
});
Handlebars 模板
<div class="rssButtons">
<a class="1">news 1</a>
<a class="2">news 2</a>
<a class="3">news 3</a>
<a class="4">news 4</a>
</div>
<div class="rssContainer">
{{#each items}}
<div class="tab" id="{{this.name}}">
{{#each data}}
<div class="news-item">
<span class="news-title">{{title}}</span>
<span class="news-publishing">{{publishingDate}}</span>
<span class="news-description">{{{rssDesc description}}}</span>
</div>
{{/each}}
</div>
{{/each}}
</div>
最佳答案
根据这个post jquery方式是最简单的
// retrieves all the text from a string of html.
jQuery(html).text();
关于javascript - 检查 RSS 源中的 HTML 标记并删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34062245/