如何使用jQuery来获取span元素的值 [英] How to grab the value of the span element using jQuery
问题描述
我有以下代码:
<tr>
<td Width="50%" align="left">
<span id="ctl00_lblTotalDesc">Ext. Subtotal</span></td>
<td Width="50%" align="right">
<span id="ctl00_lblTotalValue">100,087,000.00</span></td>
</tr>
我使用以下方法获取第二个span元素的值:
I used the following to grab the value of the 2nd span element:
spanValue = $('#ctl00_lblTotalValue').text();
但这在Spock/Geb中似乎不起作用.我收到以下错误:
But this doesn't seem to work in Spock/Geb. I get the following error:
TypeError: $(...).text is not a function
我在做什么错
如果使用$('#ctl00_lblTotalValue'),则会出现以下错误
I get the following error if I use, $('#ctl00_lblTotalValue')
[object HTMLTableElement]
Firefox控制台没有用,因此使用了Chrome控制台.
Firefox console was not useful so used Chrome console.
在Chrome浏览器中:如果我尝试$('#ctl00_lblTotalValue'),我会得到
In Chrome: if I try $('#ctl00_lblTotalValue'), I get
<span id="ctl00_lblTotalValue">100,087,000.00</span>
但是.text()-给出类型错误:对象#没有方法'text'
But .text() - gives Type error: Object # has no method 'text'
推荐答案
"ctl00_lblTotalValue"是您分配给该范围的ID还是服务器端控件,其ID由环境自动分配?
Is "ctl00_lblTotalValue" the id you assigned to the span or is it a server-side control getting its ID auto-assigned by the environment?
例如,如果我的.NET aspx页面中有此代码
For example, if I have this code in my .NET aspx page
<div id="pnlHeader" runat="server"></div>
它在html页面中显示为
it gets rendered in the html page as
<div id="ctl00_pnlHeader"></div>
如果是这种情况,我需要使用这种语法在jquery中进行抓取
If that is the case, I need to grab it in the jquery using this syntax
$("#<%= pnlHeader.ClientID%>").text()
如果您是服务器端控件,则可能需要使用来获取文本
If yours is a server-side control, you may need to grab the text using
spanValue = $("#<%= lblTotalValue.ClientID%>").text();
这篇关于如何使用jQuery来获取span元素的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!