如何从正文中反转 e.preventDefault() ?

发布于 2024-09-15 12:10:20 字数 793 浏览 1 评论 0原文

我有这个:

function dontMove(event) {
    // Prevent page from elastic scrolling
    event.preventDefault();
}

&

<body ontouchmove="dontMove(event);">

在 ipad 上,这会阻止它被拖动,并且当您拖动整个页面时,不允许 ipad 出现灰色背景。

我在另一个网站上看到,可以在另一个 div 中反转它,以便该 div 再次完全可拖动。

有谁知道如何扭转它?

我也尝试使用它来防止它(在 document.ready 中):

document.ontouchmove = function(e){
    e.preventDefault();
}

&启用它:

function doTouchMove(state) {
    document.ontouchmove = function(e){
    return state;
    }
}

然后我输入它来激活它。

<img ontouchmove="doTouchMove(state);" src="../jpeg/pages/01.jpg" class="touch"/>

这似乎不起作用 这有什么问题吗? 或者任何其他可能有效的方法?

I have this:

function dontMove(event) {
    // Prevent page from elastic scrolling
    event.preventDefault();
}

&

<body ontouchmove="dontMove(event);">

This, on the ipad, stops it from being draggable and does not allow that grey background the ipad has when you drag a whole page around to show up.

I have seen on another website that its possible to reverse that in another div, so that div is completely draggable again.

Does anyone know how to reverse it?

I have also tried using this to prevent it (in the document.ready):

document.ontouchmove = function(e){
    e.preventDefault();
}

& this to enable it:

function doTouchMove(state) {
    document.ontouchmove = function(e){
    return state;
    }
}

Then I put this to activate it.

<img ontouchmove="doTouchMove(state);" src="../jpeg/pages/01.jpg" class="touch"/>

This didn't seem to work
Is there anything wrong with this?
Or any other way that might work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

庆幸我还是我 2024-09-22 12:10:20

这正是为什么 bubbles 稍微好一点的原因(至少在我看来)。

bubbles 是跨浏览器的,所以你应该能够替换。

e.preventDefault()

使用

e.bubbles = false;

在代码中

和 后面的内容,您可能会将 bubbles 重置为 true。如果以上不是一个选项,则忽略即可。 :D

另一种选择(如果您只使用 iPad)是反转 DOM 的工作方式。

document.addEventListener('click', function(){}, true );

这将迫使事件向另一个方向发展。

Document click execute
                     |
                     |
                     v
                  Element click execute

This is exactly why bubbles is slightly better(at least in my opinion).

bubbles is cross browser, so you should be able to replace.

e.preventDefault()

with

e.bubbles = false;

and then latter in your code, you could potentially reset bubbles to true.

If the above isn't an option then just ignore. :D

An alternative(if you are just working with an iPad) is to just reverse how the DOM works.

document.addEventListener('click', function(){}, true );

This will force the event to work in the other direction.

Document click execute
                     |
                     |
                     v
                  Element click execute
猫瑾少女 2024-09-22 12:10:20

尝试这篇文章,带有 event.preventDefault 的 HTML 并从中删除 ontouchmove身体标签。

我的看起来像这样

<script>
    // Get touch move enevt from IOS
    document.ontouchmove = function (event) {
        if (!event.elementIsEnabled)
            event.preventDefault();
    };

    // Get touch move enevt from IOS
    function enableOnTouchMove(event) {
      event.elementIsEnabled = true;
    };
</script>

,然后在你想要的每个标签上启用 ontouchmove 。 IE:

<div ontouchmove="enableOnTouchMove(event)" id="listing">

try this post, HTML with event.preventDefault and erase ontouchmove from body tag.

Mine looks like this

<script>
    // Get touch move enevt from IOS
    document.ontouchmove = function (event) {
        if (!event.elementIsEnabled)
            event.preventDefault();
    };

    // Get touch move enevt from IOS
    function enableOnTouchMove(event) {
      event.elementIsEnabled = true;
    };
</script>

then enable ontouchmove on every tag you want. ie:

<div ontouchmove="enableOnTouchMove(event)" id="listing">
君勿笑 2024-09-22 12:10:20

我设法解决了它

$('#form1').unbind('submit').submit();

I managed to solve it with

$('#form1').unbind('submit').submit();
分開簡單 2024-09-22 12:10:20

您可以通过仅阻止来自主体的事件来解决此问题:

document.ontouchmove = function(event){
  if(event.target.tagName == "BODY"){
    event.preventDefault();
  }
}

You can solve it by preventing the event only if it comes from the body:

document.ontouchmove = function(event){
  if(event.target.tagName == "BODY"){
    event.preventDefault();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文