Home > Web Front-end > Front-end Q&A > How to use jquery mobile dialog box

How to use jquery mobile dialog box

WBOY
Release: 2023-05-14 09:24:37
Original
510 people have browsed it

The jQuery mobile dialog box is one of the commonly used user interaction tools in web pages. Using dialog boxes on the mobile terminal can enhance the user experience and facilitate user operations and information exchange. The following is how to use the jquery mobile dialog box.

  1. Introduce the jQuery library and dialog plug-in

Introduce the jQuery library and dialog plug-in into the project. The jQuery library can be downloaded from the official website, and the dialog plug-in can be found on GitHub Obtain. The introduction method is as follows:

<head>
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script src="path/to/dialog.js"></script>
</head>
Copy after login
  1. Create the basic HTML structure

Create the basic HTML structure, including the title, content, buttons and other elements of the dialog box. The common dialog box structure is as follows:

<div class="dialog">
    <div class="dialog-title">对话框标题</div>
    <div class="dialog-content">对话框内容</div>
    <div class="dialog-btn">
        <button class="dialog-confirm">确认</button>
        <button class="dialog-cancel">取消</button>
    </div>
</div>
Copy after login
  1. Register dialog box events

Use jQuery to register dialog box events, including the opening and closing events of the dialog box. Common dialog box events are as follows:

//显示对话框
$(".dialog").dialog("show");

//隐藏对话框
$(".dialog").dialog("hide");

//确认按钮点击事件
$(".dialog-confirm").on("click", function() {
    //执行确认操作
});

//取消按钮点击事件
$(".dialog-cancel").on("click", function() {
    //执行取消操作
});
Copy after login
  1. Customize dialog box style

Use CSS styles to customize the dialog box, including the width, height, font, and color of the dialog box and borders etc. Common dialog styles are as follows:

/*对话框样式*/
.dialog {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    border-radius: 5px;
    box-shadow: 0 0 5px #999;
    z-index: 9999;
}

/*对话框标题样式*/
.dialog-title {
    font-size: 16px;
    font-weight: bold;
    color: #333;
    padding: 10px 15px;
    border-bottom: 1px solid #eaeaea;
}

/*对话框内容样式*/
.dialog-content {
    padding: 15px;
    font-size: 14px;
    color: #666;
}

/*对话框按钮样式*/
.dialog-btn {
    text-align: center;
    padding: 10px 0;
    border-top: 1px solid #eaeaea;
}

/*确认按钮样式*/
.dialog-confirm {
    display: inline-block;
    width: 120px;
    height: 36px;
    line-height: 36px;
    background-color: #2d8cf0;
    color: #fff;
    font-size: 14px;
    border-radius: 5px;
    margin-right: 10px;
    cursor: pointer;
}

/*取消按钮样式*/
.dialog-cancel {
    display: inline-block;
    width: 120px;
    height: 36px;
    line-height: 36px;
    background-color: #eaeaea;
    color: #333;
    font-size: 14px;
    border-radius: 5px;
    cursor: pointer;
}
Copy after login
  1. Complete code example
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jQuery移动端对话框使用方法</title>
    <style>
        /*对话框样式*/
        .dialog {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: #fff;
            border-radius: 5px;
            box-shadow: 0 0 5px #999;
            z-index: 9999;
        }

        /*对话框标题样式*/
        .dialog-title {
            font-size: 16px;
            font-weight: bold;
            color: #333;
            padding: 10px 15px;
            border-bottom: 1px solid #eaeaea;
        }

        /*对话框内容样式*/
        .dialog-content {
            padding: 15px;
            font-size: 14px;
            color: #666;
        }

        /*对话框按钮样式*/
        .dialog-btn {
            text-align: center;
            padding: 10px 0;
            border-top: 1px solid #eaeaea;
        }

        /*确认按钮样式*/
        .dialog-confirm {
            display: inline-block;
            width: 120px;
            height: 36px;
            line-height: 36px;
            background-color: #2d8cf0;
            color: #fff;
            font-size: 14px;
            border-radius: 5px;
            margin-right: 10px;
            cursor: pointer;
        }

        /*取消按钮样式*/
        .dialog-cancel {
            display: inline-block;
            width: 120px;
            height: 36px;
            line-height: 36px;
            background-color: #eaeaea;
            color: #333;
            font-size: 14px;
            border-radius: 5px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="dialog" style="display: none">
        <div class="dialog-title">对话框标题</div>
        <div class="dialog-content">对话框内容</div>
        <div class="dialog-btn">
            <button class="dialog-confirm">确认</button>
            <button class="dialog-cancel">取消</button>
        </div>
    </div>
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script src="path/to/dialog.js"></script>
    <script>
        $(function() {
            //显示对话框
            $(".dialog").dialog("show");

            //隐藏对话框
            $(".dialog").dialog("hide");

            //确认按钮点击事件
            $(".dialog-confirm").on("click", function() {
                //执行确认操作
            });

            //取消按钮点击事件
            $(".dialog-cancel").on("click", function() {
                //执行取消操作
            });
        });
    </script>
</body>
</html>
Copy after login

The above is how to use the jquery mobile dialog box. Through simple steps and code examples, you can Quickly use dialog plug-ins in projects to enhance user experience and improve website quality.

The above is the detailed content of How to use jquery mobile dialog box. For more information, please follow other related articles on the PHP Chinese website!

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