“开"防止违约 [英] "on" preventDefault

查看:16
本文介绍了“开"防止违约的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里错过了什么?

请注意:使用 jQuery MOBILE

Please note: jQuery MOBILE is used

DEMO 使用 return false

如果我使用 preventDefault 页面加载,就好像我只有链接而没有脚本一样,当我更改为返回 false(我总是在普通 JS onclick 事件处理程序上使用)时,它会按预期工作.我已经浏览了其他帖子,并且都使用 .click 并且都建议 preventDefault.

If I use preventDefault the page loads as if I had just links and no script, when I change to return false (which I always used to use on the plain JS onclick event handler), it works as expected. I have already looked through other posts and all use .click and all suggest preventDefault.

$(document).ready(function() {
  $("#leftdiv a").on("click",function(e) {
    $("#rightDiv").load(this.href);
    return false; // I was sure preventDefault would work
  });
});

HTML

<div id="leftdiv" style="position:absolute;padding-right:5%; overflow:scroll;">
<a href="page1.htm">Launch page 1</a><br />
<a href="page2.htm">Launch page 2</a>
</div>

<div id="rightDiv" style="padding-left:30%"></div>

<div id="rightDiv" style="padding-left:30%"></div>

推荐答案

didn't work ?

没用?

$('a').on("click",function(e){
     //do something
     e.preventDefault();
});

e.preventDefault(); 放在代码的顶部或末尾通常无关紧要.我猜还有方法 e.stop() .但是,当 return false; 对您有用时,您为什么不坚持使用它?

Putting e.preventDefault(); on the top or on the end of your code does not matter normally. There is also the method e.stop() I guess. But why don't you stick with return false; when it's working for you ?

来自 jQuery 移动文档:

取消元素默认点击行为

应用程序可以在 vclick 事件上调用 preventDefault() 来取消元素的默认点击行为.在基于鼠标的设备上,调用vclick 事件上的 preventDefault() 等同于调用 preventDefault()在气泡事件阶段的真实点击事件上.基于触摸设备,它有点复杂,因为实际的点击事件是在调度 vclick 事件后大约 300 毫秒调度.触控用设备,在 vclick 事件上调用 preventDefault() 会触发一些代码在尝试捕捉下一个点击事件的 vmouse 插件中在捕获事件阶段由浏览器调度,并且对其调用 preventDefault() 和 stopPropagation() .如中所述上面的警告,有时很难匹配一个触摸事件与其对应的鼠标事件,因为目标可能不同.为了出于这个原因,vmouse 插件也回退到尝试通过坐标识别对应的点击事件.仍然有目标和坐标识别都失败的情况,这导致点击事件被分派并触发元素的默认操作,或者在内容已经移动或替换,触发对不同元素的点击.如果对于给定的元素/控件,这种情况会定期发生,我们建议您使用点击来触发您的操作.

Applications can call preventDefault() on a vclick event to cancel an element's default click behavior. On mouse based devices, calling preventDefault() on a vclick event equates to calling preventDefault() on the real click event during the bubble event phase. On touch based devices, it's a bit more complicated since the actual click event is dispatched about 300ms after the vclick event is dispatched. For touch devices, calling preventDefault() on a vclick event triggers some code in the vmouse plugin that attempts to catch the next click event that gets dispatched by the browser, during the capture event phase, and calls preventDefault() and stopPropagation() on it. As mentioned in the warning above, it is sometimes difficult to match up a touch event with its corresponding mouse event because the targets can differ. For this reason, the vmouse plugin also falls back to attempting to identify a corresponding click event by coordinates. There are still cases where both target and coordinate identification fail, which results in the click event being dispatched and either triggering the default action of the element, or in the case where content has been shifted or replaced, triggering a click on a different element. If this happens on a regular basis for a given element/control, we suggest you use click for triggering your action.

function() {
  return false;
}

// IS EQUAL TO

function(e) {
  e.preventDefault();
  e.stopPropagation();
}

来源: http://css-tricks.com/return-false-and-prevent-default/

似乎也支持 .on() ,因为不支持 document.ready

It also seems that .on() isn supported also as document.ready is not supported

来源: http://jquerymobile.com/test/docs/api/事件.html

这篇关于“开"防止违约的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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