Javascript 如何禁用 onclick 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4725242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I disable the onclick event
提问by tonoslfx
How do I disable the onclick event?
I've tried onclick="this.disabled=true;"
, but it doesn't work.
如何禁用 onclick 事件?
我试过了onclick="this.disabled=true;"
,但它不起作用。
Here is an HTML table:
这是一个 HTML 表格:
<table>
<tr>
<td onclick="parent.location='home.php'">Available</td>
<td onclick="parent.location='home.php'">Available</td>
</tr>
<tr>
<td onclick="parent.location='home.php'"><div onclick="this.disabled=true;">Booked</div></td>
<td onclick="parent.location='home.php'">Available</td>
</tr>
</table>
Using the above code it is still going to home.php, even if i click on the table cell marked `Booked'.
使用上面的代码它仍然会转到 home.php,即使我单击标记为“已预订”的表格单元格。
回答by Shadow Wizard is Ear For You
You need to prevent the event from bubbling upwards.. most simple way:
您需要防止事件向上冒泡..最简单的方法:
<div onclick="event.cancelBubble = true;">Booked</div>
Tested successfully for IE8, Chrome and Firefox.
已针对 IE8、Chrome 和 Firefox 成功测试。
回答by Nanne
you should overwrite the onclick with a function that returns false(as in: "the click is consumed"). So you'd do something like
您应该使用返回false的函数覆盖 onclick (如:“点击被消耗”)。所以你会做类似的事情
onclick="return false;"
wait. i meant false:)
等待。我的意思是假的:)
回答by DerekH
Personally, I think you would be best served to take the 'onclick' off of the TD and place it on the div within it(similar to how you have booked). Then you can decide in your PHP logic whether to put an 'onclick' on the div or leave it off(you wouldn't need to try to override it then).
就个人而言,我认为您最好将 'onclick' 从 TD 上取下并将其放在其中的 div 上(类似于您的预订方式)。然后,您可以在 PHP 逻辑中决定是在 div 上放置“onclick”还是将其关闭(那时您不需要尝试覆盖它)。