2012-06-26 30 views
1

我有一个带有类和几个表行的表(所有这些都是类,因为多个类似的表是从相同的代码生成的),我想从中获取一些信息其中包含的具有标题的表格数据。它看起来是这样的:带有标题的表格数据的Jquery选择器

<table class='mytable'><br/> 
<tr>< td></td></tr><br/> 
<tr class ='useful'><td title='hasTitle'>9</td></tr><br/> 
</table><br/> 

(在空间添加到HTML,因为我不知道如何得到它显示的变量。)

是否有jQuery的方式选择9(所以表内数据的文本)基于表类和表行和td标题?我会这样认为,但我不确定。我猜它会看起来像

$('#mytable tr#useful td[title='hasTitle']).html(); 

但我不知道如果该选择符语法是否正确或不。

+0

尝试'$文本();'''#是'id',其中作为'.'用于类http://api.jquery.com/class-selector/ –

+0

#mytable引用元素的名称。你的表没有名字,myTable是类。类在它们前面有一段时间,所以$('。myTable')将返回表。另外,在HTML中你应该使用双引号:例如class =“mytable” –

回答

1
$('.mytable tr.useful td[title="hasTitle"]').html(); 

正如你音符class,对于表中没有id,所以你应该使用.而不是#

阅读关于jQuery的id selectorclass selector

0
$('.mytable tr.useful td[title="hasTitle"]').html() 

jsFiddle example

当您应该引用类时,您正在引用ID。 #选择一个ID,而.选择一个类。

另一种方法是使用你的代码(用小帖校正,这是$('#mytable tr#useful td[title="hasTitle"]').html();),和你的表的HTML更改为:

<table id='mytable'><br/> 
<tr><td></td></tr><br/> 
<tr id='useful'><td title='hasTitle'>9</td></tr> 
</table>​ 
0

选择器jQuery是像“CSS”你写的:

$('#mytable tr#useful td[title='hasTitle']).html(); 

在这里,您正在寻找与ID “MYTABLE” 里面一个TR的对象....

你应该写类似:( “mytable的tr.useful TD [标题= 'hasTitle']”)。

$('table.mytable tr.useful td[title="hasTitle"]').html();