简体   繁体   English

如何获取跨度标签的值?

[英]how to get the value of a span tag?

I want to get the contents of a span tag but what I have isn't working.我想获取 span 标签的内容,但我所拥有的内容不起作用。 An alert shows an empty dialog box.警报显示一个空对话框。 I've tried the following but nothing works.我已经尝试了以下但没有任何效果。

var test=$('#test').val()
var test=$('span#test').val()
var test=$('td span#test').val()

Can someone tell me what I'm doing wrong?有人可以告诉我我做错了什么吗?

span elements do not have a value property. span元素没有value属性。

Instead, use html() for the HTML or text() for the text nodes.相反,对 HTML 使用html()或对文本节点使用text()

$('span#test').text()

will give you the text.会给你文字。 val() is for the attribute value val() 用于属性值

You are correct almost, but instead of the val() to Need to say html().您几乎是正确的,但不是 val() 而是需要说 html()。

 var test=$('#test').html();
 var test=$('span#test').html();
 var test=$('td span#test').html();

a span doesn't have a value attribute.跨度没有值属性。

You should instead do the following您应该改为执行以下操作

var test=$('span#test').html()

or或者

var test=$('span#test').text()

use this用这个

var test=$('#test').html()

You should use .text() .您应该使用.text() .val() is only for input elements. .val()仅适用于输入元素。

var test = $('span#test').text();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM