我目前有一个非常疯狂的代码,可以在动态表中单击和展开行。我的问题是我需要使表中的最后一列不可点击,我不知道该怎么做。我搜索过互联网并尝试了不同的东西,但似乎没有任何帮助。
这是我的表:
<table id="myTable">
<tr>
<th>Name</th>
<th>Type</th>
<th>Details</th>
</tr>
{{#each companies}}
<tr class="record">
<td>
{{this.name}}
</td>
<td>
{{this.type}}
</td>
<td id="details">
<img style="height: 20px; width: 20px" src="img/arrow_right.png">
</td>
</tr>
{{/each}}
</table>
我想让最后一列(ID =“details”)无法点击。
这是我疯狂的点击事件:
$("#myTable").on("click", "tr.record", function () {
var rowIndex = this.rowIndex,
services = null;
var self = this;
$.ajax({
url: "api/relation",
type: 'GET',
success: function (response) {
if (response[rowIndex - 1].relationStats) {
services = response[rowIndex - 1].relationStats.PROVIDES_SERVICE;
}
if (services > 0 && $(self).hasClass('record active')) {
console.log("CLOSING");
$(self.nextSibling).remove();
$(self).removeClass('active');
} else if (services > 0) {
console.log("OPENING");
var openDetails = $('<tr/>').addClass('openDetails').insertAfter(self);
$(self).addClass('active');
var td = $('<td/>').addClass('td').appendTo(openDetails);
$('<p/>').html("Provides service(s): ").appendTo(td);
$('<td/>').html(services).appendTo(openDetails);
}
}
});
});
任何帮助将不胜感激!
答案 0 :(得分:3)
而不是将点击处理程序注册到tr
使用td
并过滤掉最后一个孩子
$("#myTable").on("click", "td:not(:last-child)", function () {
var self = $(this).closest('tr').get(0), rowIndex = self.rowIndex,
services = null;
$.ajax({
url: "api/relation",
type: 'GET',
success: function (response) {
if (response[rowIndex - 1].relationStats) {
services = response[rowIndex - 1].relationStats.PROVIDES_SERVICE;
}
if (services > 0 && $(self).hasClass('record active')) {
console.log("CLOSING");
$(self.nextSibling).remove();
$(self).removeClass('active');
} else if (services > 0) {
console.log("OPENING");
var openDetails = $('<tr/>').addClass('openDetails').insertAfter(self);
$(self).addClass('active');
var td = $('<td/>').addClass('td').appendTo(openDetails);
$('<p/>').html("Provides service(s): ").appendTo(td);
$('<td/>').html(services).appendTo(openDetails);
}
}
});
});
答案 1 :(得分:0)
$("#myTable").on("click", "#details", function (e) {
e.stopPropagation();
});
答案 2 :(得分:0)
我认为你应该使用
$("#myTable").on("click", "td:not(:last)", function () {.....}
而不是not(:last-child)
,如果情况是:
<tr>
<td>....</td>
<td>....</td>
<td class="not record">....</td>
</tr>
<tr>
<td>....</td>
<td>....</td>
<td class="record">....</td>
</tr>