preventDefault无法在Google Chrome 5中运行

Arm*_*ndo 1 javascript google-chrome

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

这是表格.

<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>
Run Code Online (Sandbox Code Playgroud)

我正在使用事件委派来管理点击.这是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;
 }

}
Run Code Online (Sandbox Code Playgroud)

任何建议,将不胜感激.

Tim*_*own 5

preventDefault()在这种类型的DOM 0事件处理程序中,这不是防止浏览器默认行为的最佳方法,因为它当然不能在IE和其他浏览器中工作.只需使用return false;.适用于所有浏览器.

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;
 }
}
Run Code Online (Sandbox Code Playgroud)