需要 jQuery text() 函数来忽略隐藏元素
我有一个 div 设置如下:
<div id="test"> <p>Hello</p> <p style="display: none">Goodbye</p> </div>
编辑:为了澄清,这是最简单的例子。 div 可以有任意数量的 n 个深层嵌套子级。
$('#test').getText()
返回“Hello Goodbye”。下面是在 Firebug 中测试的一行代码: jQuery('
Hello
这似乎是因为 jQuery 内部使用的 textContent(对于非 IE)返回隐藏元素作为文本的一部分。哼。
有没有办法返回文本内容而忽略 display:none'd 元素?基本上,我试图模仿用鼠标突出显示 div 并复制到系统剪贴板所获得的文本。这会忽略隐藏文本。
有趣的是,如果您创建一个选择范围并从中获取文本,也会返回 display:none 元素内的文本。
var range = document.body.createTextRange();
range.moveToElementText($('#test')[0]);
range.select();
console.log(range.toString()); // Also logs Hello Goodbye!
因此,就 display:none 元素而言,创建文档选择范围似乎与使用鼠标突出显示的效果不同。我该如何解决这个肮脏的泡菜难题?
编辑:建议使用 .filter(':visible').text
,但它不适用于这种情况。我需要返回的文本与鼠标选择的文本完全相同。例如:
$('<div>test1 <p>test2</p>\r\n <b>test3</b> <span style="display:none">none</span></div>').appendTo(document.body).children().filter(':visible').text()
返回
"test2test3"
当我真正想要的输出是
test1 test2
test3
换行符、空格和所有内容时,它们来自 \r\n
I have a div set up something like this:
<div id="test"> <p>Hello</p> <p style="display: none">Goodbye</p> </div>
EDIT: To clarify, this is the simplest example. The div could have any arbitrary number of n deep nested children.
$('#test').getText()
returns 'Hello Goodbye'. Here's a one liner to test in Firebug: jQuery('<div id="test"> <p>Hello</p> <p style="display: none">Goodbye</p> </div>').text()
This seems to be because what jQuery uses internally, textContent (for non IE), returns hidden elements as part of the text. Hrmph.
Is there a way to return the text content ignoring display:none'd elements? Basically I am trying to mimic the text you would get from highlighting the div with your mouse and copying to system clipboard. That ignores hidden text.
Interestingly, if you create a selection range and get the text from it, that also returns text inside display:none elements.
var range = document.body.createTextRange();
range.moveToElementText($('#test')[0]);
range.select();
console.log(range.toString()); // Also logs Hello Goodbye!
So creating a document selection range doesn't appear to do the same thing as highlighting with the mouse in terms of display:none elements. How do I get around this dirty pickle conundrum?
Edit: using .filter(':visible').text
has been suggested, but it won't work for this scenario. I need the returned text to be EXACTLY what would come from a selection with the mouse. So for example:
$('<div>test1 <p>test2</p>\r\n <b>test3</b> <span style="display:none">none</span></div>').appendTo(document.body).children().filter(':visible').text()
returns
"test2test3"
When the output I actually want is
test1 test2
test3
linebreaks, whitespace and all, which come from the \r\n
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用
.filter(":visible")
过滤元素。或者使用这个:
但是 jQuery 文档 建议我们使用
.filter()< /code> 相反:
因为
:visible
是 jQuery 扩展,而不是 CSS 规范的一部分,使用
:visible
的查询无法利用本机 DOMquerySelectorAll()
方法提供的性能提升。为了在使用 :visible 选择元素时获得最佳性能,请首先使用纯 CSS 选择器选择元素,然后使用.filter(":visible")
。Filter the elements using
.filter(":visible")
.Or use this:
But the jQuery documentation advises us to use
.filter()
instead:Because
:visible
is a jQuery extension and not part of the CSS specification,queries using
:visible
cannot take advantage of the performance boost provided by the native DOMquerySelectorAll()
method. To achieve the best performance when using :visible to select elements, first select the elements using a pure CSS selector, then use.filter(":visible")
.在选择器中使用
:visible
,如下所示:函数示例:
-- 编辑:
http:// jsfiddle.net/8H5ka/ (适用于 Chrome,它在结果中显示“Hello”)
如果上述方法不起作用:
http://jsfiddle.net/userdude/8H5ka/1/
Use
:visible
in your selector as such:A Function example:
-- Edit:
http://jsfiddle.net/8H5ka/ ( Works on Chrome it displays "Hello" in Result )
If the above doesn't work:
http://jsfiddle.net/userdude/8H5ka/1/
如果空间不是主要问题,您可以复制标记,删除隐藏元素,然后输出该文本。
If space isn't a major concern you could copy the markup, remove the hidden elements, and output that text.
我搜索并发现了这个问题,但没有解决方案。
对我来说,解决方案就是摆脱 jquery 使用 DOM:
或者如果选择器数组中的元素超过一个,您需要一个 for 循环和合并,但我想大多数时候它是您需要的第一个版本。
I search for that and found this question but without solution.
Solution for me is just get out of jquery to use DOM:
or if more than on element in array of selector, you need a for loop and a merge but I guess that most of time it is the first version that you need.
我遇到了这个问题并发现了这个问题,似乎实际的解决方案是基于提供的答案,但实际上并没有写出来。因此,这里有一个适合我的情况的完整解决方案,它与 OP 相同,但附加规定,由于基于 DOM 位置的外部样式,元素可能不可见。示例:
解决方案是:
#test
从 DOM 中取出,jQuery 可能不知道它们是不可见的,因为它们将不再符合 CSS 规则。代码:
I had this problem and found this question, and it seems the actual solution is based on the provided answers but not actually written out. So here's a complete solution that worked for my situation, which is the same as the OP with the additional provision that elements may be invisible due to external styles based on DOM position. Example:
The solution is to:
#test
out of the DOM before we remove invisible objects, jQuery might not know they're invisible because they will no longer match the CSS rules.The code:
以下是我使用 MooTools 的方法:
Here is how I did it with MooTools: