$(function() {
var message = $('#message');
var tr = $('#tbl').find('tr');
tr.bind('click', function(event) {
var values = '';
tr.removeClass('row-highlight');
var tds = $(this).addClass('row-highlight').find('td');
$.each(tds, function(index, item) {
values = values + 'td' + (index + 1) + ':' + item.innerHTML + '<br/>';
});
message.html(values);
});
});
鼠标移到:
var message = $('#message');
var tr = $('#tbl').find('tr');
tr.hover(
function() { // mouseover
$(this).addClass('row-highlight');
var values = '';
var tds = $(this).find('td');
$.each(tds, function(index, item) {
values = values + 'td' + (index + 1) + ':' + item.innerHTML + '<br/>';
});
message.html(values);
},
function() { // mouseout
$(this).removeClass('row-highlight');
message.html('');
}
);
});
定制 TD:
<table id ="table1">
<th>MAHDI</th>
<th>PISHGUY</th>
<tr>
<td id = "1">username</td>
<td id = "2">666</td>
</tr>
</table>
$('#table1 tr').each(function (){
var vara = $(this).find('#2').html();
if(vara != null)
alert($(this).find('#2').html());
});
或者:
<tr class="rowid">
<td class="user">A</td>
<td class="id">B</td>
</tr>
$(document).ready(function(){
rows = $(".rowid").each(function(index,value){
user = $(value).children(".user").text();
id = $(value).children(".id").text();
alert(id+'\t'+user);
});
});