jQuery获取页面中所有元素的文本 [英] jQuery get the text of all elements in a page
本文介绍了jQuery获取页面中所有元素的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取所有元素的文本.我在这里使用此代码:
I want to get the text of all elements. I am using this code here:
$('*').filter(function()
{
if(($(this).text().lenght>0)&&($(this).text().lenght<100))
{
return true;
}
else
{
return false;
}
}).each(function()
{
console.log($(this).text());
});
我尝试仅显示短文本,因为.text()有时返回html代码,但根本无法正常工作.
I have try to show only short text because .text() sometime returns html code but it is not working at all.
推荐答案
简单得多:$('body').text()
为您提供页面上的整个文本.
It's much more simple: $('body').text()
gives you the whole text on the page.
如果您需要遍历所有文本节点,请参见此处:如何使用jQuery选择文本节点?
If you need to iterate over all the text nodes, see here: How do I select text nodes with jQuery?
这篇关于jQuery获取页面中所有元素的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文