从绑定元素的子元素中删除事件处理程序
我想将单击处理程序应用于表中的行:
var myTable = $(this);
myTable.on('expand', 'tr', myHandler);
myTable.on('click', 'tr', function () {
$(this).trigger('expand');
});
function myHandler(e) {
// my action
}
这是一个非常简化的版本,但它会触发一个扩展行的 jQuery 方法。我想根据特定行的内容从特定行中删除此处理程序。我有一个每次单击该行时都会触发的函数。如果它与设置的条件不匹配,则会触发一个方法,显示一条消息,通知用户该问题。我现在想禁用该行的单击展开。
function stopFunction(row) {
row.find('th[scope=row]').parents("#tableID").off('expand', 'tr', myHandler);
}
上面只是删除了所有行中的单击以展开功能的操作。我不知道如何允许单击其他行,但只是从这个单个表行中删除处理程序。请注意,row.find('th[scope]') 是该行的表格单元格,该行的第一个父级是该行,然后该行的父级是该表(@tableID )。
I have click handlers I want to apply to the rows in my table:
var myTable = $(this);
myTable.on('expand', 'tr', myHandler);
myTable.on('click', 'tr', function () {
$(this).trigger('expand');
});
function myHandler(e) {
// my action
}
This is a much simplified version, but it fires a jQuery method that expands the rows. I want to remove this handler from particular rows depending on their contents. I have a function that is fired every time the row is clicked. If it does not match set conditions, a method is fired displaying a message informing the user of the issue. I want to now disable the click to expand of this row.
function stopFunction(row) {
row.find('th[scope=row]').parents("#tableID").off('expand', 'tr', myHandler);
}
The above just removes the click to expand capabilities from all rows. I don't know how to go about allowing the clicking of other rows, but just removing the handler from this single table row. Please note, the row.find('th[scope]')
is the table cell of the row, the first parent of that is it's row, and then the row's parent is the table (@tableID).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
概念证明。
Proof of concept.
正如您所发现的,您的
.off
调用将关闭整个事件处理程序。这是因为,使用on
(这是一种很好的做法),您只有一个事件处理程序,它响应所有匹配的元素(在您的情况下是tr
)。我建议您改为使用类来执行此操作。将类
no-expand
添加到那些不应扩展的tr
中,并改用此事件处理程序:As you have found out, your
.off
call will turn off the whole event handler. That's because, usingon
(which is a good practice), you only have one event handler, which responds to all matching elements (tr
in your case).I suggest you do this using classes instead. Adding a class
no-expand
to thosetr
s which shouldn't expand, and using this event handler instead: