首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JQuery获取以元素开头的文本

JQuery获取以元素开头的文本
EN

Stack Overflow用户
提问于 2012-03-21 19:59:27
回答 1查看 362关注 0票数 1

我有这个代码。

代码语言:javascript
复制
<p>jQuery is free, open source software, dual-licensed under the <span>MIT License</span> or the GNU General Public License, Version 2.</p>
<p><span id="elt">jQuery is a</span> cross-browser <span>JavaScript</span>library designed to simplify the client-side scripting of HTML. <span>It was released in January 2006</span> at BarCamp NYC by John Resig. Used by over 52% of the 10,000 most visited websites, jQuery is the most popular JavaScript library in use <span>today</span>.</p>
<img src="character1.jpg" height="200"/>
<p>jQuery is free, open source software, dual-licensed under the <span>MIT License</span> or the GNU General Public License, Version 2.</p>
<p>...

我希望前500个字符以$(".elt")开头,如下所示:

代码语言:javascript
复制
jQuery is a cross-browser JavaScript library designed to simplify the client-side scripting of HTML.It was released in January 2006 at BarCamp NYC by John Resig. Used by over 52% of the 10,000 most visited websites, jQuery is the most popular JavaScript library in use today.jQuery is free, open source software, dual-licensed under the MIT License or the GNU General Public License, Version 2.

只是文本,去掉所有的html标签。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-21 20:01:23

代码语言:javascript
复制
$('#elt').parent().text().slice(0, 500);

获取文本,取前500个字符

编辑:对不起,请阅读问题而不是代码示例。已修复。

代码语言:javascript
复制
var node = $('#elt').parent();
var text = node.text();

while (text.length < 500) {
    node = node.nextSibling;
    if (node.nodeType === 1) {
        text += node.text();
    } else if (node.nodeType === 3) {
        text += node.nodeValue;
    }
}

text = text.slice(0, 500);
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9804204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档