CSS/JavaScript:获取元素的用户可见文本

发布于 2024-08-23 09:31:25 字数 533 浏览 3 评论 0原文

假设我有以下 HTML:

<div id="test">
  <span style="display:inline;">foo</span>
  <span style="display:none;">bar</span>
  <span style="display:inline;">baz</span>
</div>

.. JavaScript 中是否有某种方法可以让我获得输出“foo baz”(而不是“foo bar baz”)?

$('test').textContent 返回后者,而 innerHTML 执行等效操作。

我根本不在乎所使用的方法是否是hackish或迂回的,并且如果它是特定于浏览器的或需要Flash,我可以处理它。

但是,它不能要求除 JS 或 Flash 之外的任何内容,不能要求任何用户交互,并且不能返回“bar”。

有想法吗?

Suppose I have the following HTML:

<div id="test">
  <span style="display:inline;">foo</span>
  <span style="display:none;">bar</span>
  <span style="display:inline;">baz</span>
</div>

.. is there some way in JavaScript for me to get the output "foo baz" (not "foo bar baz")?

$('test').textContent returns the latter, and innerHTML does the equivalent.

I don't care at all if the method used is hackish or roundabout, and can deal with it if it's browser-specific or requires Flash.

However, it must not require anything other than JS or Flash, it must not require any user interaction, and it must not return 'bar'.

Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹏雨一样淡蓝的深情 2024-08-30 09:31:25

您可以执行此操作,但请注意,它不会像您的示例那样有空格,因为标记中没有空格:

$("#test :visible").text()

这是另一种选择,就像您的示例为每个跨度间隔开一样:

var s = new Array();
$("#test :visible").each(function() {
   s.push($(this).text());
});
alert(s.join(' '));

You can do this, but note it won't have a space like your example, because there is no space in the markup:

$("#test :visible").text()

Here's an alternative, like your example spaced out for each span:

var s = new Array();
$("#test :visible").each(function() {
   s.push($(this).text());
});
alert(s.join(' '));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文