Javascript 获取 Span Text 的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10343838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:57:24  来源:igfitidea点击:

Get value of Span Text

javascripthtml

提问by Hunter Mitchell

I have a span with class="span"and a hidden field class="dropdown".

我有一个 spanclass="span"和一个隐藏字段class="dropdown"

The spantext changes, and I need to grab the text and set it as the value of the hidden field's value.

span文本的变化,我需要获取文本,并将其设置为隐藏字段的值的值。

I will then use php (I already have) and use the name of the hidden field to email me the text.

然后我将使用 php(我已经有了)并使用隐藏字段的名称将文本通过电子邮件发送给我。

How do I do it?

我该怎么做?

回答by John Conde

<script type="text/javascript">
document.getElementById('button1').onChange = function () {
    document.getElementById('hidden_field_id').value = document.getElementById('span_id').innerHTML;
}
</script>

回答by maxspan

var span_Text = document.getElementById("span_Id").innerText;

console.log(span_Text)
<span id="span_Id">I am the Text </span>

回答by wle8300

The accepted answer is close... but no cigar!

接受的答案很接近......但没有雪茄!

Use textContentinstead of innerHTMLif you strictly want a string to be returned to you.

如果您严格希望将字符串返回给您,请使用textContent代替innerHTML

innerHTMLcan have the side effect of giving you a node element if there's other dom elements in there. textContentwill guard against this possibility.

innerHTML如果那里有其他 dom 元素,可能会给你一个节点元素的副作用。textContent将防范这种可能性。

回答by Vishal Thakur

You need to change your code as below:

您需要更改您的代码如下:

<html>
<body>

<span id="span_Id">Click the button to display the content.</span>

<button onclick="displayDate()">Click Me</button>

<script>
function displayDate() {
   var span_Text = document.getElementById("span_Id").innerText;
   alert (span_Text);
}
</script>
</body>
</html>

回答by Hyman

Judging by your other post: How to Get the inner text of a span in PHP. You're quite new to web programming, and need to learn about the differences between code on the client (JavaScript) and code on the server (PHP).

根据您的其他帖子判断:如何在 PHP 中获取 span 的内部文本。您是 Web 编程的新手,需要了解客户端代码 (JavaScript) 和服务器代码 (PHP) 之间的区别。

As for the correct approach to grabbing the span text from the client I recommend Johns answer.

至于从客户端获取跨度文本的正确方法,我建议约翰回答。

These are a good place to get started.

这些都是开始的好地方。

JavaScript: https://stackoverflow.com/questions/11246/best-resources-to-learn-javascript

JavaScript:https: //stackoverflow.com/questions/11246/best-resources-to-learn-javascript

PHP: https://stackoverflow.com/questions/772349/what-is-a-good-online-tutorial-for-php

PHP:https: //stackoverflow.com/questions/772349/what-is-a-good-online-tutorial-for-php

Also I recommend using jQuery (Once you've got some JavaScript practice) it will eliminate most of the cross-browser compatability issues that you're going to have. But don't use it as a crutch to learn on, it's good to understand JavaScript too. http://jquery.com/

此外,我建议使用 jQuery(一旦您有一些 JavaScript 练习),它将消除您将遇到的大多数跨浏览器兼容性问题。但是不要把它当作学习的拐杖,理解 JavaScript 也很好。 http://jquery.com/