问题描述
背景
我一直在努力解决这个愚蠢的小问题,即当在 jQuery Mobile 应用程序中的锚元素上触发 click 事件时尝试取消链接方向.
假设我有一个这样的简单多页文档:
... 和 JavaScript 是这样的:
(function (MyApp, $, undefined) {'使用严格';//初始化应用函数初始化(){$('#mLink').on('click', function (event) {event.preventDefault();//event.stopPropagation();//event.stopImmediatePropagation();});}//jQuery Mobile 现在准备好了 ->覆盖默认值$(document).on("mobileinit", function () {//设置默认页面过渡$.mobile.defaultPageTransition = '幻灯片';});//jQuery Mobile 现在准备好了$(document).ready(init);}(window.MyApp = window.MyApp || {}, jQuery));
问题
我只是不明白为什么 event.preventDefault() 没有取消页面转换.但是,如果我添加 event.stopPropagation() 它将取消它.此外,如果我从应用程序中删除 jQuery Mobile 库,它可以在没有 event.stopPropagation() 的情况下工作.
问题
这是正常行为吗?是否可以在处理程序中始终调用它们来取消链接方向?
备注
我使用 jQuery Mobile 只是因为它的多页模板、导航和转换功能.
其他问题
潜伏在我原来的问题后面的问题是'jQuery Mobile 对拦截 event.preventDefault() 方法以不阻止链接的默认操作的链接做了什么,即转到目标页面.?'
解决方案 正常吗?是的.两个都可以打电话吗?是的.
event.preventDefault() 是防止浏览器默认行为的唯一工具,event.stopPropagation() 是防止竞争事件处理程序触发的唯一工具.
如果你想要一个单行,你可以使用 return false 来达到同样的效果.来自 http://api.jquery.com/on/:
<块引用>从事件处理程序返回 false 将自动调用event.stopPropagation() 和event.preventDefault().也可以为handler 作为 function(){ return false; 的简写}.所以,$("a.disabled").on("点击", false);将事件处理程序附加到所有带有禁用"类的链接,阻止它们被跟踪当它们被点击时,也会阻止事件冒泡.
Background
I've been struggling with this stupid little problem of trying to cancel link direction when click event is fired on an anchor element in jQuery Mobile app.
Let's say I have a simple multi-page document like this:
<div data-role="page" id="page-1">
<div data-role="content">
<a href="#page-2" id="mLink">page 2</a>
</div><!-- /content -->
</div><!-- /page -->
<div data-role="page" id="page-2">
<div data-role="content">
</div><!-- /content -->
</div><!-- /page -->
... and JavaScript like this:
(function (MyApp, $, undefined) {
'use strict';
// Initializes app
function init() {
$('#mLink').on('click', function (event) {
event.preventDefault();
//event.stopPropagation();
//event.stopImmediatePropagation();
});
}
// jQuery Mobile is ready now -> override defaults
$(document).on("mobileinit", function () {
// Set the default page transition
$.mobile.defaultPageTransition = 'slide';
});
// jQuery Mobile is ready now
$(document).ready(init);
}(window.MyApp = window.MyApp || {}, jQuery));
Problem
I just can't figure out why event.preventDefault() isn't canceling the page transition. However, if I add event.stopPropagation() it will cancel it. Also if I remove the jQuery Mobile library from the app, it works without event.stopPropagation().
Question
Is this normal behavior and is it ok to always call them both in the handler to cancel the link direction?
Notes
I'm using jQuery Mobile only for it's multipage template, navigation and transition capabilities.
Additional question
The question lurking behind my original question kind of was 'what jQuery Mobile does to a link that intercepts event.preventDefault() method to not prevent the link's default action, i.e. going to the target page.?'
解决方案 Normal? Yes. OK to call both? Yes.
event.preventDefault() is the only tool you have to prevent the browser's default behavior and event.stopPropagation() is the only tool you have to prevent competing event handlers from firing.
If you want a one-liner, you can use return false to achieve the same result. From http://api.jquery.com/on/:
Returning false from an event handler will automatically call
event.stopPropagation() and
event.preventDefault(). A false value can also be passed for the
handler as a shorthand for function(){ return false; }. So,
$("a.disabled").on("click", false); attaches an event handler to all
links with class "disabled" that prevents them from being followed
when they are clicked and also stops the event from bubbling.
这篇关于event.preventDefault() 不取消 jQuery Mobile 中的链接方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End
相关推荐
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22
前端开发问题
182
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23
前端开发问题
262
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18
前端开发问题
301
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18
前端开发问题
125
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17
前端开发问题
437
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11
前端开发问题
455
热门文章
1错误 [ERR_REQUIRE_ESM]:不支持 ES 模块的 require()
2vue中yarn install报错:info There appears to be trouble with you
3为什么 Chrome(在 Electron 内部)会突然重定向到 chrome-error://chromewebdat
4“aria-hidden 元素不包含可聚焦元素"显示模态时的问题
5使用选择器在 CSS 中选择元素的前一个兄弟
6js报错:Uncaught SyntaxError: Unexpected string
7layui怎么刷新当前页面?
8将模式设置为“no-cors"时使用 fetch 访问 API 时出错
热门精品源码
最新VIP资源
1多功能实用站长工具箱html功能模板
2多风格简历在线生成程序网页模板
3论文相似度查询系统源码
4响应式旅游景点宣传推广页面模板
5在线起名宣传推广网站源码
6酷黑微信小程序网站开发宣传页模板
7房产销售交易中介网站模板
8小学作业自动生成程序


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)