使用jquery取消桌面上的onclick事件

时间:2009-09-23 13:20:22

标签: jquery

我在表中添加了一个jquery onclick事件处理程序,如下所示

$('#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.

}

如何取消桌面上的点击事件?

2 个答案:

答案 0 :(得分:2)

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

所以你的代码应该是:


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

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

编辑:听起来真的想要像stopPropagation()这样的东西,但反之亦然。因此,不是一个孩子停止父母接收的事件,而是试图让父母阻止孩子接收事件。

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


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

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

答案 1 :(得分:0)

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

您可以禁用表格内的文本框。

相关问题