event.preventDefault() 不取消 jQuery Mobile 中的链接方向

2023-04-19前端开发问题
3

本文介绍了event.preventDefault() 不取消 jQuery Mobile 中的链接方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

背景
我一直在努力解决这个愚蠢的小问题,即当在 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

相关推荐

js删除数组中指定元素的5种方法
在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中表单会自动刷新的问题,因为用到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

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

jQuery怎么动态向页面添加代码?
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)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455