使用jquery取消表上的onclick事件

使用jquery取消表上的onclick事件,jquery,Jquery,我向表中添加了jquery onclick eventhandler,如下所示 $('#tableid').bind('click', ClickHandler); function ClickHandler(event) { //here i am checking whether the cell that the user clicked is valid to enter data. //if not, I would like to cancel the event.

我向表中添加了jquery onclick eventhandler,如下所示

$('#tableid').bind('click', ClickHandler);

function ClickHandler(event) {

   //here i am checking whether the cell that the user clicked is valid to enter data.
   //if not, I would like to cancel the event.

   e.preventDefault(); //this is not working. the cell that the user clicked still has focus.
   return false; //this is also not working. the cell that the user clicked still has focus.

}

如何取消表上的单击事件?

看起来您在错误的对象上调用了preventDefault()。处理程序接受一个参数“event”,但您使用的是“e”

因此,您的代码应该是:


$('#tableid').bind('click', ClickHandler);

function ClickHandler(event) {
   event.preventDefault(); // event not e
   return false;
}
编辑:听起来好像你真的想要这样的东西,但事实正好相反。因此,与其让孩子停止家长接收的事件,不如让家长首先让孩子停止接收事件

我想你可能想要更像这样的东西:


$('#tableid *').bind('click', ClickHandler); // note the * to apply this to all children

function ClickHandler(event) {
   event.preventDefault(); // event not e
   return false;
}

您可以在取消单击事件的表顶部显示一个不可见的div。但是为什么你想要一个文本框,看起来你可以点击它,但实际上你不能


您可以禁用表中的文本框。

单元格中是否有可编辑控件来获取焦点,或者是否有比您提到的更多的内容?我在单元格中有一个文本框。文本框正在获得焦点,我想取消它。我还添加了keydown、keypress、dblclick和keyup事件来处理表导航。我试过了,但没有成功。细胞仍然接收焦点。