我的HTML是:
<body id="bodyMain">
<table id="myTable" class="table-bordered">
<thead>
<tr>
<td>UserId</td>
<td>UserName</td>
<td>FirstName</td>
<td>LastName</td>
<td>PhoneNumber</td>
<td>Address</td>
<td>Email Id</td>
<td>UserType</td>
</tr>
</thead>
<tbody></tbody>
</table>
我有一个与表myTable关联的click事件。它使表格单元可编辑。现在,我需要在整个正文中附加一个事件,检查单击的区域是否为表,然后使用表单击创建的文本框将消失,单元格值将取而代之。js代码是
var globalFlag = '';
var prevValue;
var prevHandle = null;
$('#myTable').on('click', 'tbody tr td', function (e) {
debugger;
console.log($(this).text());
var rowIndex = $(this).parent('tr').index();
var colIndex = $(this).index();
var index = '' + rowIndex + ',' + colIndex;
console.log(index);
if (globalFlag != index && colIndex != 0) {
//remove initially created textbox
if (prevHandle != null) {
prevHandle.find('input[type="text"]').remove();
prevHandle.text(prevValue);
}
prevHandle = $(this);
prevValue = $(this).text();
globalFlag = index;
var valuepresent = $(this).text();
var txtsize = parseInt($(this).width());
console.log(txtsize);
var txt = "<input type='text' value='" + $(this).text() +
"'style='width:" + txtsize + "px'>";
$(this).text('');
$(this).append(txt);
}
});
$('body').on("click", function () {
debugger;
var th = $(this).attr('id');
var tb = $('#myTable');
if (th != null || th != undefined)
if (th.indexOf("myTable") >= 0)
return;
if (prevHandle != null) {
prevHandle.find('input[type="text"]').remove();
prevHandle.text(prevValue);
}
});
无论如何,我试着把这两个事件一个接一个地称为。我需要的是,如果点击区域不是表,然后禁用编辑单元格数据。
$('body >:not(#myTable)').on("click", function () {