Build interactive web applications using html

Learning objectives: Understand the basic concepts, characteristics and usage of HTML, and be able to use HTML to create interactive Web applications.

Learning content:
1. What is htmx?
   - htmx is a JavaScript library for building interactive web applications.
   - It extends HTML into a declarative and interactive language, allowing developers to use simple HTML tags to achieve dynamic page updates, real-time data loading and form submission without refreshing the page.

2. Features of htmx:
   - Lightweight: The htmx library is very small, only a few KB in size, so it can be loaded and used quickly.
   - Easy to use: htmx uses HTML attributes to define interactive behavior without writing complex JavaScript code.
   - Strong compatibility: htmx can be used in conjunction with any back-end technology (such as Python, Ruby, Java, etc.) and front-end frameworks (such as React, Vue, etc.).
   - Highly flexible: htmx provides various options and configurations, allowing developers to customize the interaction behavior according to their needs.

3. Basic usage of htmx:
   - Attributes: htmx uses HTML attributes to define interactive behaviors. Commonly used attributes include `hx-get`, `hx-post`, `hx-swap`, etc.
   - Events: htmx supports various events, such as `hx-click`, `hx-change`, etc., which can trigger page updates or data loading.
   - Data update: htmx can obtain data from the server through AJAX requests and update the data to the specified area on the page without refreshing the entire page.
   - Form submission: htmx can implement form submission without refreshing the page. The form data can be sent to the server through the `hx-post` attribute, and the returned data can be updated on the page.

For example, here is a simple example using html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>htmx 任务列表示例</title>
    <!-- 引入 htmx 库 -->
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/htmx.js"></script>
</head>
<body>
    <h1>任务列表</h1>

    <!-- 添加任务表单 -->
    <form id="add-task-form" hx-post="/add-task" hx-target="#task-list">
        <input type="text" name="task" placeholder="添加新任务" required>
        <button type="submit">添加</button>
    </form>

    <!-- 任务列表 -->
    <ul id="task-list">
        <!-- 任务项将在这里动态添加 -->
    </ul>

    <!-- htmx 用于处理点击事件 -->
    <script>
        // 当页面加载时,初始化 htmx
        document.addEventListener('DOMContentLoaded', function () {
            htmx.engine();
        });
    </script>
</body>
</html>

In this example, we create a simple task list application that includes a form to add tasks and a task list. Using HTML we can easily add interactivity to these elements.

Server-side code (assuming Python and Flask framework):

from flask import Flask, request, render_template_string

app = Flask(__name__)

# 初始任务列表
tasks = []

@app.route('/')
def index():
    return render_template_string(open('index.html').read())

@app.route('/add-task', methods=['POST'])
def add_task():
    task = request.form.get('task')
    if task:
        tasks.append(task)
    return render_template_string('<li>{
   
   { task }}</li>', task=task)

if __name__ == '__main__':
    app.run(debug=True)

This simple example demonstrates how to use HTML to handle form submission and dynamically update a task list. When the user enters a task in the add task form and clicks the "Add" button, htmx sends the task to the server and appends the new task item to the task list without refreshing the entire page. This approach can greatly improve user experience while reducing the writing of front-end JavaScript code. Server-side code uses the Flask framework to handle requests and responses

By learning and mastering the basic concepts, features, and usage of html, you can use simple HTML tags to create interactive web applications, improve user experience and reduce the need for page refreshes.

Guess you like

Origin blog.csdn.net/canduecho/article/details/132639380