This question already has an answer here:
这个问题已经有了答案:
- Get value of div content using jquery 5 answers
- 使用jquery 5答案获取div内容的值
I am new to JQuery and dynamically appending a value to an h1 element using a span element
我是JQuery新手,使用span元素动态地向h1元素添加值
<h1>Height: <span class="height"></span></h1>
Now I want to take the value of the span element and use it somewhere else. I tried doing this in JQuery but it's no good.
现在我想取span元素的值并把它用在别的地方。我试着用JQuery做这个,但是没有用。
var height = $(".height").val();
What is the right way instead?
正确的方法是什么?
3 个解决方案
#1
3
A span
element doesn't have a "value", form elements do. (input
, select
, etc.) If you're looking to get the text of the span
element, you can do that like so:
span元素没有“值”,而表单元素有。(输入、选择等)如果您想获取span元素的文本,可以这样做:
var height = $(".height").text();
Note: If there are multiple matching elements, this will concatenate the text of all of them. Observe.
注意:如果有多个匹配元素,这将连接所有元素的文本。观察。
#2
0
change your code like this:
更改您的代码如下:
var height = $(".height").text();
note:
注意:
val ( ) -> for input element(like: textarea, input etc.)
val() ->表示输入元素(如:textarea, input等)
#3
0
Please use this.
请用这个。
Var height=$(".height").text();
#1
3
A span
element doesn't have a "value", form elements do. (input
, select
, etc.) If you're looking to get the text of the span
element, you can do that like so:
span元素没有“值”,而表单元素有。(输入、选择等)如果您想获取span元素的文本,可以这样做:
var height = $(".height").text();
Note: If there are multiple matching elements, this will concatenate the text of all of them. Observe.
注意:如果有多个匹配元素,这将连接所有元素的文本。观察。
#2
0
change your code like this:
更改您的代码如下:
var height = $(".height").text();
note:
注意:
val ( ) -> for input element(like: textarea, input etc.)
val() ->表示输入元素(如:textarea, input等)
#3
0
Please use this.
请用这个。
Var height=$(".height").text();