-1

CodeIgniterのテーブルクラスを使用して生成されたテーブルがあるページを取得しました。<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>

タグをクリックしたときに、タグ内のコンテンツ(テキスト)を取得するにはどうすればよいですか?

4

4 に答える 4

3
$('table td').click(function() { 
    var text = $(this).text(); 
});
于 2013-03-13T17:39:12.180 に答える
2

これを使って:

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

または:

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

フィドル

于 2013-03-13T17:39:20.377 に答える
1

私はリンクを使用することを好みます、それはよりユーザーフレンドリーです:

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(); 
});
于 2013-03-13T17:47:41.407 に答える
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*/
});

デモのために私のフィドルを見る

于 2013-03-13T17:44:58.213 に答える