2016-11-15 34 views
0

你好,我想爲每個錶行設置一個onclick jQuery中,但我不知道從哪裏開始,我吮吸搜索,所以我希望在這裏得到一些幫助。 我已經試過這設置onclick爲childnodes jquery或javascript

var tableRows = $("tableID").children(); 
console.log(tableRows); 

但執行console.log(tableRows)只返回上一個對象r.fn.init [0],所以我想我沒有什麼從一起工作。我怎樣才能得到一個數組中的tableRows,並通過for循環運行它們?

+0

'我吸在searching'你不會得到任何更好,如果你不練 –

+0

搜索點擊事件處理 –

+0

https://開頭的API .jquery.com/click/ –

回答

1

試試這個:

$('document').ready(function() { 
 
    var tableRows = $("#tableID td"); 
 
    tableRows.on('click', function(e) { 
 
    console.log('%s %s %s', e.target.tagName, e.target.innerHTML, "clicked"); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<table id="tableID"> 
 
    <tr> 
 
    <td>Row1</td> 
 
    </tr> 
 
    <tr> 
 
    <td>Row2</td> 
 
    </tr> 
 
</table>

+0

迄今爲止,這項工作相當順利,謝謝。 – Spygol