admin管理员组文章数量:1406950
How can I get plain text from an element?
<div id="summary_header">
<span class="heading" style="margin-left: 210px;">This is first line</span><br/>
<span style="margin-left: 260px;"><b> You are in second line</b></span><br/>
</div>
What's the way to get plain text from this div
tag? I want to get plain text as "This is first line You are in second line".
$('summary_header').textContent
gives with white spaces.
How can I get plain text from an element?
<div id="summary_header">
<span class="heading" style="margin-left: 210px;">This is first line</span><br/>
<span style="margin-left: 260px;"><b> You are in second line</b></span><br/>
</div>
What's the way to get plain text from this div
tag? I want to get plain text as "This is first line You are in second line".
$('summary_header').textContent
gives with white spaces.
- Is their is any method in prototypeJs like jQuery text() method. – prasannaboga Commented Jan 20, 2011 at 9:15
2 Answers
Reset to default 4If you need a method, no problem. That is the beauty of javascript and it's prototypal inheritance.
Prototype.js allows you to extend the Element object with new methods. Just add this to your scripts:
Element.addMethods({
getText: function(element) {
element = $(element);
return element.innerHTML.strip().stripTags().replace(/\n/g,' ').replace(/\s+/g,' ');
}
});
That is almost the same as @koko proposed, but I suggest changing each new line character \n
to one space instead of removing them, and changing any number of consecutive withe-space characters to single space. This is similar behavior to what browsers do.
When you at that to your scripts, after the prototype.js loads, you can use new method to get plain text:
var plainText = $('summary_header').getText();
It's not very beautiful, but it works. I don't know what you're trying to achieve, but there you go:
var f = $('summary_header').innerHTML.strip().stripTags().replace(/\n/g,'').replace(/\s{2}/g,'');
http://jsfiddle/e8uFS/1/
本文标签: javascriptHow to get plain text from an element in PrototypeJSStack Overflow
版权声明:本文标题:javascript - How to get plain text from an element in PrototypeJS? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744947874a2633896.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论