如何使用JavaScript/jQuery禁用滚动更改字段中的数字

如何使用JavaScript/jQuery禁用滚动更改字段中的数字

给定包含属性的HTML文档,任务是使用JavaScript/jQuery禁用鼠标滚轮更改数字值。

方法:

  • 选择元素。
  • 为该元素添加一个事件,当滚动开始时调用一个函数。
  • 在滚动时禁用该元素。

示例1: 以下示例使用JavaScript实现上述方法。

<!DOCTYPE HTML>
<html>
 
<head>
    <title>
        How to disable scroll to change number
        value in input type="number"
        attribute in JavaScript/jQuery?
    </title>
</head>
 
<body style="text-align:center;">
 
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
 
    <h3>
        Click on the button to disable
        scrolling on input element
    </h3>
 
    <form>
        <input id="input" type="number" />
    </form>
    <br>
 
    <button onclick="GFG_Fun()">
        Click Here
    </button>
 
    <h3 id="GFG" style="color: green;"></h3>
 
    <script>
        let result = document.getElementById("GFG");
 
        function GFG_Fun() {
            let input = document.getElementById("input");
 
            input.addEventListener("mousewheel",
                function (event) {
                    this.blur()
                });
 
            result.innerHTML = "Mouse wheel disabled!";
        }
    </script>
</body>
 
</html>

输出:

如何使用JavaScript/jQuery禁用滚动更改字段中的数字

示例 2: 使用jQuery实现上述方法的示例。

<!DOCTYPE HTML>
<html>
 
<head>
    <title>
        How to disable scroll to change number 
        value in input type="number" attribute
        in JavaScript/jQuery?
    </title>
 
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
    </script>
</head>
 
<body style="text-align:center;">
 
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
 
    <h3>
        Click on the button to disable 
        scrolling on input element
    </h3>
 
    <form>
        <input id="input" type="number" />
    </form>
 
    <br>
 
    <button onclick="GFG_FUN()">
        Click Here
    </button>
 
    <h3 id="GFG" style="color: green;">
    </h3>
 
    <script>
        let elm = document.getElementById("GFG");
 
        function GFG_FUN() {
            ('#input').on("wheel", function (e) {
                (this).blur();
            });
 
            el_down.innerHTML = "Mouse wheel disabled!";
        }
    </script>
</body>
 
</html>

输出:

如何使用JavaScript/jQuery禁用滚动更改字段中的数字

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程