我在使用 event.preventDefault(); 时遇到问题获取我的 DOM 上的 ahref
我在 DOM 上使用 event.preventDefault();
作为 ahref
时遇到问题。
如何防止 url 发布 nofollow 删除(如使用 JQuery 的 HTML 指定的那样)?
<td class="trash_can">
<a rel="nofollow" data-remote="true" data-method="delete" data-confirm="Are you sure you want to delete Greek Theater at U.C. Berkeley?" href="/promotions/2/places/46">
<img id="trash_can" src="http://test.dev/images/trash.png?1305741883" alt="Trash">
以下代码应该会破坏 HTML,但 HTML 在发出“您确定吗”警告之前发布删除操作。没有什么不工作:
$(function(){
$('.trash_can a').click(function(event) {
event.preventDefault();
console.log('Clicked Delete');
});
});
I'm having trouble using the event.preventDefault();
for an ahref
on my DOM.
How do you prevent the url from posting a nofollow delete, as specified by the HTML using JQuery?
<td class="trash_can">
<a rel="nofollow" data-remote="true" data-method="delete" data-confirm="Are you sure you want to delete Greek Theater at U.C. Berkeley?" href="/promotions/2/places/46">
<img id="trash_can" src="http://test.dev/images/trash.png?1305741883" alt="Trash">
The following code should subvert the HTML but the HTML posts a delete action, prior to giving a "are you sure" warning. nothing is not working:
$(function(){
$('.trash_can a').click(function(event) {
event.preventDefault();
console.log('Clicked Delete');
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能不仅需要
event.preventDefault()
,还需要执行return false
。 PreventDefault() 会阻止默认操作,但事件仍然会冒泡,可能会到达下一个处理程序,这可能是 Fredrik 指出的 Rails 处理程序:It is possible that instead of just
event.preventDefault()
, you need to doreturn false
also or instead. preventDefault() prevents the default action, but the event still bubbles up, possibly to the next handler, which could be the rails handler that Fredrik pointed out:你用的是导轨吗?看起来是这样。
event.preventDefault()
不起作用的原因是因为魔法是在其他地方完成的。Rails 附带了一些帮助程序,可以为 Rails 生成的所有“删除”操作创建事件处理程序。因此,就您的情况而言,我认为您需要找出如何覆盖帮助程序,或者只是为该特定删除操作创建自定义事件处理程序。
这是 Rails 使用的 JQuery 代码:
https://github。 com/rails/jquery-ujs/blob/master/src/rails.js
基于此(来自链接):
如果您删除 data-remote,您的事件处理程序将是唯一执行的事件处理程序,来自链接的
data-method
和data-confirm
。因此,我建议您删除它们,然后在您自己的点击处理程序中重新创建您想要的任何行为。
Are you using rails? Looks like it.
The reason
event.preventDefault()
doesn't work is because the magic is done elsewhere.Rails comes with some helpers that creates event handlers for all the "delete" actions generated by Rails. So in your case I think that you need to find out how to override the helpers or just create a custom event handler for that specific delete action.
This is the JQuery code that Rails uses:
https://github.com/rails/jquery-ujs/blob/master/src/rails.js
Based on this (from the link):
Your event handler will be the only one executed if you'd remove
data-remote
,data-method
anddata-confirm
from the link.So I suggest that you remove them and then just re-create whatever behavior you want in your own click handler.
我遇到了同样的问题,但在打破了我的头几个小时后未能得到任何好的解决方案。因此,根据此处建议的答案,我做了类似的事情:
我承认这不是一个好的解决方法。但是,这就是对我的帮助。
I faced the same issue, but failed to get any good solution after breaking my head for hours. So, based on the suggested answers here, I did something like:
I accept it's not a good workaround. But, thats what helped me.
这是一个老问题,但如果您将 Rails 5.1+ 与 vanilla UJS 而不是 jQuery 一起使用,则
event.preventDefault()
或从该方法返回false
都不会停止 Rails -ujs 处理它。您需要显式调用Rails.stopEverything(事件)
It's an old question, but if you are using Rails 5.1+ with the vanilla UJS instead of jQuery one, neither
event.preventDefault()
nor returningfalse
from the method stops rails-ujs from handling it. You need to explicitly callRails.stopEverything(event)