当我单击 HTML 表格中的一行时,我想从之前单击的行中删除 highlightedRow
类,并将其添加到新行中。
$(function () {
$("table tr").click(function (e) {
//remove class
var dataTable = $("#TemplateData");
dataTable.removeClass("highlightedRow");
//add class
var dTable = $(this);
dTable.addClass("highlightedRow");
g_previouslyClickedRow = $(this).index()
}
问题是:
var dTable = $(this);
dTable.addClass("highlightedRow");
正在获取行元素,同时:
var dataTable = $("#TemplateData");
dataTable.removeClass("highlightedRow");
正在获取表格元素。
如何使用表格元素和 previouslySelectedRow
值?
g_previouslyClickedRow = $(this).index()
获取行元素?
最佳答案
您需要将正确的元素
定位到removeClass
$("table tr").click(function (e) {
//remove class
$('.highlightedRow').removeClass('highlightedRow');
//add class
$(this).addClass("highlightedRow");
}
当你说:
var dataTable = $("#TemplateData");
//dataTable will have reference to your table if #TemplateData is the id of your table
dataTable.removeClass("highlightedRow");
//the above line will just remove class from table as you have stored reference of your table
//in dataTable
当你说:
var dTable = $(this);
//$(this) will be referring to current element context inside click
dTable.addClass("highlightedRow");
//and thus the clicked element, whose reference is stored in dTable, will get the class
//highlightedRow.
关于javascript - 删除表中单行的类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33015916/