您可以通過結合click
和mouseover
事件給每個td
容易jQuery。
$("table-selector tr td").each(function(){
$(this).click(function(){
// TODO with click
}).hover(function(){
// TODO with mouseover
},function(){
// TODO with mouseout
});
}
UPDATE:分離細胞指數和行索引和保存主題爲每個td
「s的數據:
$("#myTable tr").each(function(r){
var row = r;
$("td", this).each(function(d){
var cell = d;
$(this)
.data("rowIndex", row)
.data("cellIndex", cell)
.click(function(){
$("#message").text("Row-Index is: " + $(this).data("rowIndex") +
" and Cell-Index is: " + $(this).data("cellIndex"));
})
.hover(
function(){
$(this).addClass("td-over").css({"text-align":"center"});
},function(){
$(this).removeClass("td-over").css({"text-align":"left"});
});
});
});
See the full-demo here at jsfiddle
我想說使用jQuery – Petah