Home > Web Front-end > JS Tutorial > body text

How to add click and double-click events to an element through jQuery

藏色散人
Release: 2021-08-27 11:09:30
Original
2515 people have browsed it

In the previous article "Use JavaScript to delete the last item from the array (3 methods)", I introduced you how to use JavaScript to delete the last item from the array. Interested friends can Go learn and understand~

The important content explained in this article is the method of adding click and double-click events to elements through jQuery.

In this article we will add click and double-click events through the bind() method. The bind() method adds one or more event handlers to the selected element, as well as a function to run when the event occurs. In addition, we will add the result to the element using the appendTo() method, which inserts the specified content at the end (still inside) of the selected element.

Let’s go directly to the code:

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            $(".clickable_ele").bind("click", function () {
                $("<h4>调用单击事件</h4>")
                    .appendTo(".res");
            });

            $(".clickable_ele").bind("dblclick", function () {
                $("<h4>调用双击事件</h4>")
                    .appendTo(".res");
            });
        });
    </script>
    <style>
        body {
            text-align: center;
        }

        h1 {
            color: #ff311f;
        }

        .clickable_ele {
            font-size: 20px;
            font-weight: bold;
            color: #ff7800;
        }

    </style>
</head>

<body>
<h1>PHP中文网</h1>

<h3>
    如何给一个元素添加单击和双击事件?
</h3>

<div class="clickable_ele">
    可点击的元素
</div>

<div class="res"></div>
</body>
</html>
Copy after login

The effect is as follows:

GIF 2021-8-27 星期五 上午 10-58-23.gif

  • ##bind() syntax is

    $(selector).bind(event,data,function,map)

  • 参数分别表示:
    event必需。规定添加到元素的一个或多个事件。
    由空格分隔多个事件值。必须是有效的事件。
    data可选。规定传递到函数的额外数据。
    function必需。规定当事件发生时运行的函数。
    map规定事件映射 ({event:function, event:function, ...}),包含要添加到元素的一个或多个事件,以及当事件发生时运行的函数。
    Copy after login
  • appendTo() syntax is

    $(content) .appendTo(selector)

    ##
    参数分别表示:
    content必需。规定要插入的内容(可包含 HTML 标签)。
    selector必需。规定把内容追加到哪个元素上。
    Copy after login
    Note:

    Since jQuery version 1.7, the on() method adds event handling to the selected element The preferred method of the program.

    The append() and appendTo() methods perform the same task; the differences are: the positioning of the content and selector, and the ability of append() to use functions to append content.

    Finally, I would like to recommend "

    JavaScript Basics Tutorial" ~ Welcome everyone to learn ~

    The above is the detailed content of How to add click and double-click events to an element through jQuery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!