我无法使用event.preventDefault();为我的DOM上的一个ahref [英] I'm having trouble using the event.preventDefault(); for an ahref on my DOM

查看:222
本文介绍了我无法使用event.preventDefault();为我的DOM上的一个ahref的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的DOM上使用 event.preventDefault(); ahref 中遇到麻烦。 / p>

如何防止url发布nofollow删除,如HTML使用JQuery指定的?

 < td class =trash_can> 
< a rel =nofollowdata-remote =truedata-method =deletedata-confirm =你确定要删除美国联邦大学伯克利分校的希腊剧院吗? HREF = /促销/ 2 /地点/ 46​​ >
< img id =trash_cansrc =http://test.dev/images/trash.png?1305741883alt =垃圾桶>

以下代码应该颠覆HTML,但HTML发布删除操作之前,会发出是你确定警告。没有什么不工作:

  $(function(){
$('。trash_can a')。 function(event){
event.preventDefault();
console.log('Clicked Delete');
});
});


解决方案

有可能,而不是 event.preventDefault(),您还需要执行 return false 。 preventDefault()阻止默认动作,但事件仍然起起伏伏,可能是下一个处理程序,这可能是Fredrik指出的rails处理程序:

  $(rails.linkClickSelector).live('click.rails',function(e){
var link = $(this);
if(!rails.allowAction ))return rails.stopEverything(e);

if(link.data('remote')!== undefined){
rails.handleRemote(link);
return false;
} else if(link.data('method')){
rails.handleMethod(link);
return false;
}
});


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');
  });
});

解决方案

It is possible that instead of just event.preventDefault(), you need to do return 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:

 $(rails.linkClickSelector).live('click.rails', function(e) {
    var link = $(this);
    if (!rails.allowAction(link)) return rails.stopEverything(e);

    if (link.data('remote') !== undefined) {
      rails.handleRemote(link);
      return false;
    } else if (link.data('method')) {
      rails.handleMethod(link);
      return false;
    }
  });

这篇关于我无法使用event.preventDefault();为我的DOM上的一个ahref的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆