preventDefault无法在Google Chrome 5中使用 [英] preventDefault not working in Google Chrome 5

查看:134
本文介绍了preventDefault无法在Google Chrome 5中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想停止重新加载页面的表单提交按钮。这适用于Firefox 3.6,Safari 5和Opera。但是Chrome 5并没有阻止这种链接。

I want to stop a form submit button from reloading a page. This works in Firefox 3.6, Safari 5, and Opera. But Chrome 5 does not prevent the link.

以下是表单。

Here is the form.

<form class="container_7" id="find-store-all" action=" ">
 <label for="customer-loc" class="grid_3">Find a Store Near You:</label>
 <input name="customer-loc" id="customer-loc" class="grid_3" type="text" placeholder="zip code or address"  />
    <button type="submit" id="customer-loc-sub" class="grid_1" >Enter</button>
</form>

我正在使用事件委托管理点击。这里是JavaScript。

I am using event delegation to manage the click. Here is the JavaScript.

document.onclick = handleClick;

function handleClick(e){
 if(!e) var e = window.event; 
 var target = e.target || e.srcElement;

 switch(target.id){
  case "customer-loc-sub" :
  e.preventDefault();
  findClosestStoreOne();
  break;
 }

}

任何建议,将不胜感激。 / p>

Any suggestions would be appreciated.

推荐答案

preventDefault()并不是防止浏览器这种类型的DOM 0事件处理程序中的默认行为,因为它在IE和其他浏览器中肯定不起作用。只需使用 return false; 代替。适用于所有浏览器。

preventDefault() isn't the best way to prevent the browser's default behaviour in this type of DOM 0 event handler, as it certainly doesn't work in IE and possibly other browsers too. Just use return false; instead. Works in all browsers.

document.onclick = handleClick;

function handleClick(e){
 if(!e) var e = window.event; 
 var target = e.target || e.srcElement;

 switch(target.id){
  case "customer-loc-sub" :
  findClosestStoreOne();
  return false;
 }
}

这篇关于preventDefault无法在Google Chrome 5中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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