使用JQuery禁用拖动滚动文本输入 [英] Disabling Drag Scroll for Text Inputs with JQuery

查看:157
本文介绍了使用JQuery禁用拖动滚动文本输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在点击文本输入时禁用拖动滚动功能。页面的格式为带有输入内容的div表,您可以在单击并拖动任何地方时水平拖动页面。我想要这样做,所以您只能在不点击其中一个输入时拖动,并在单击输入时禁用拖动,以便可以编辑输入。这里是我去的:

I need to disable the drag scroll functionality when clicking on text inputs. The page is in the format of a table of a div with inputs inside, and you can drag the page horizontally when you click and drag anywhere. I want to make this so you can only drag when you don't click on one of the inputs, and disable the drag when an input is clicked so the input can be edited. Here is my go at it:

$('.dataContent').mousedown( function (event) { 
        if($(this).children().size()>0) {$(this)
        .data('down', true)
        .data('x', event.clientX)
        .data('scrollLeft', this.scrollLeft);

        return false;
        }


推荐答案

您可以使用 event.target 以获取作为事件的原始目标的元素:

You can use event.target to obtain the element that was the event's original target:

$('.dataContent').mousedown(function(event) {
    if ($(event.target).is("input:text")) {
        return;  // Target element is a text input, do not initiate drag.
    }

    // ...
});

这篇关于使用JQuery禁用拖动滚动文本输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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