gpt4 book ai didi

javascript - JQuery:tr onblur 或 tr 边界之外的任何事件

转载 作者:行者123 更新时间:2023-12-03 04:57:36 25 4
gpt4 key购买 nike

如何使用 tr onblur 事件调用函数或如何检测 tr 边界外的任何点击事件?

谢谢

HTML:

<tr id="tr_0" class="table" onblur="Dict[@i].Canc(@j)">
<td id="frst_0" class="tabl">
<label id="lbl0_0" class="tdLbl0_0">Some name</label>
<input id="inpt0_0" class="tdinpt0" type="text" size="10" hidden onblur="Dict[@i].CheckInput0(@j)" />
</td>
<td id="sec_0" class="tabl" onclick="Dict[@i].EditInput1(@j)">
<label id="lbl1_0" class="tdLbl1">Value</label>
<input id="inpt1_0" class="tdinpt1" type="number" max="99" min="0" style="width: 38px" hidden onblur="Dict[@i].CheckInput1(@j)" onfocus="Dict[@i].ClearInput1(@j)" />
</td>
<td>
<button id="suc_0" class="tdbtn" hidden onclick="Dict[@i].Suc(@j)">Success</button>
<button id="edit_0" class="tdbtn" onclick="Dict[@i].Edit(@j)">Edit</button>
<button id="del_0" class="tdbtn" onclick="Dict[@i].Del(@j)">Delete</button>
<button id="canc_0" class="tdbtn" onclick="Dict[@i].Canc(@j)" hidden>Cancel</button>
</td>
</tr>

最佳答案

使用 jQuery.closest 查看目标元素是否位于 trtr 内,如下所示:

$(document).click(function(e) {
if($(e.target).closest("tr").length)
alert("tr was clicked!");
else
alert("no tr was clicked!");
});

如果您只想检查单击的元素是否为tr,则使用jQuery.is,如下所示:

$(document).click(function(e) {
if($(e.target).is("tr"))
alert("tr was clicked!");
else
alert("no tr was clicked!");
});

第一种情况的示例:

$(document).click(function(e) {
if ($(e.target).closest("tr").length) // if the length is != 0 (a tr is found)
alert("tr was clicked!");
else // otherwise ...
alert("no tr was clicked!");
});
tr {
background: #F99;
height: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr id="tr_0" class="table" onblur="Dict[@i].Canc(@j)">
<td id="frst_0" class="tabl">
<label id="lbl0_0" class="tdLbl0_0">Some name</label>
<input id="inpt0_0" class="tdinpt0" type="text" size="10" hidden onblur="Dict[@i].CheckInput0(@j)" />
</td>
<td id="sec_0" class="tabl" onclick="Dict[@i].EditInput1(@j)">
<label id="lbl1_0" class="tdLbl1">Value</label>
<input id="inpt1_0" class="tdinpt1" type="number" max="99" min="0" style="width: 38px" hidden onblur="Dict[@i].CheckInput1(@j)" onfocus="Dict[@i].ClearInput1(@j)" />
</td>
<td>
<button id="suc_0" class="tdbtn" hidden onclick="Dict[@i].Suc(@j)">Success</button>
<button id="edit_0" class="tdbtn" onclick="Dict[@i].Edit(@j)">Edit</button>
<button id="del_0" class="tdbtn" onclick="Dict[@i].Del(@j)">Delete</button>
<button id="canc_0" class="tdbtn" onclick="Dict[@i].Canc(@j)" hidden>Cancel</button>
</td>
</tr>
</table>

第二种情况的示例:

$(document).click(function(e) {
if ($(e.target).is("tr")) // if the target of the click is a tr
alert("tr was clicked!");
else // otherwise
alert("no tr was clicked!");
});
tr {
background: #F99;
height: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr id="tr_0" class="table" onblur="Dict[@i].Canc(@j)">
<td id="frst_0" class="tabl">
<label id="lbl0_0" class="tdLbl0_0">Some name</label>
<input id="inpt0_0" class="tdinpt0" type="text" size="10" hidden onblur="Dict[@i].CheckInput0(@j)" />
</td>
<td id="sec_0" class="tabl" onclick="Dict[@i].EditInput1(@j)">
<label id="lbl1_0" class="tdLbl1">Value</label>
<input id="inpt1_0" class="tdinpt1" type="number" max="99" min="0" style="width: 38px" hidden onblur="Dict[@i].CheckInput1(@j)" onfocus="Dict[@i].ClearInput1(@j)" />
</td>
<td>
<button id="suc_0" class="tdbtn" hidden onclick="Dict[@i].Suc(@j)">Success</button>
<button id="edit_0" class="tdbtn" onclick="Dict[@i].Edit(@j)">Edit</button>
<button id="del_0" class="tdbtn" onclick="Dict[@i].Del(@j)">Delete</button>
<button id="canc_0" class="tdbtn" onclick="Dict[@i].Canc(@j)" hidden>Cancel</button>
</td>
</tr>
</table>

关于javascript - JQuery:tr onblur 或 tr 边界之外的任何事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42369843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com