2013-03-13 72 views
-1

我得到一个页面,其中有一个使用CodeIgniter表类生成的表。 现在可以通过点击使用JQuery来获取<td>标签的内容。 例如 我的表是这样的:通过鼠标点击从JQuery表中获取数据

<table> 
<th>ID</th><th>Name</th><th>Age</th> 
<tr><td>25</td><td>Jack</td><td>15</td></tr> 
<tr><td>20</td><td>Jill</td><td>16</td></tr> 
</table> 

我怎样才能获得标签里面的内容(文本)被点击的时候吗?

+0

我做不明白什么是 – 2013-03-13 19:12:28

+0

问题是我怎样才能得到​​标签内的内容(文本),当我点击它 – muttalebm 2013-03-13 19:29:59

回答

3
$('table td').click(function() { 
    var text = $(this).text(); 
}); 
+0

谢谢你明白了! – muttalebm 2013-03-13 18:02:24

2

使用此:

$("table td").click(function(){ 
    alert($(this).text()); 
}); 

或者:

$("table td").click(function(){ 
    alert(this.innerText); 
}); 

Fiddle

1

你可以做这样的...

的JS:

$('table td').click(function() { 
var mvalue = $(this).text(); /*you can store in in a variable and use it for something else later*/ 
alert(mvalue); /*this provides a popup on top of the screen*/ 
console.log(mvalue); /*this shows you the value in your web console in case you are debugging*/ 
}); 

查看我的fiddle用于演示

1

我会prefere使用环节,其多种用户友好:

HTML:

... 
<tr> 
    <td><a href="#">25</a></td> 
    <td><a href="#">Jack</a></td> 
    <td><a href="#"15</a></td> 
</tr> 
... 

的javascript:

$('a').click(function(event) { 
    event.preventDefault(); 
    var text = $(this).text(); 
});