How jQuery dynamically controls page elements
This time I will show you how jQuery dynamically controls page elements. What are the precautions for jQuery to dynamically control page elements. The following is a practical case, let's take a look.
background
Recently I built a small system in which the user's friends need to be added, deleted, modified and checked on the page. It is not that complicated originally and can be implemented relatively easily in table form.
However, considering the user experience, we try not to use input when adding first, so we display all users in categories, and then click to add.
The added users are also displayed on the interface, showing the user's current friends. At the same time, click on the added friend to proceed with the next business operation.
Of course, the deletion operation is the same as on the mobile phone. There is a red "x" in the upper right corner. Click on the friend to delete it.
Finally, the interface can exit the deleted mode and return to the normal mode.
Function Description
1. Add user: Click to add a user in the list, and add onclick event
2. Delete user: Click on the friend to delete
3. Enter delete Mode: Change the interface to delete mode, switch onclick event
4. Restore normal mode: Change the interface to normal mode, switch onclick event
Code
//添加用户为自己常用好友 function Add(e) { var friend_id = e.id; var name = $("#" + friend_id).html(); //将要插入页面的好友html代码 var content = "<p id=\"friend" + friend_id + "\" class=\"case-item\" onclick=\"" + game_type + "(this)\"><p class=\"ih-item circle effect1\"><a href=\"#\"><img class=\"img_wrong\" src=\"image/wrong.png\" style=\"float: right; width: 15px; height: 15px;display:none\" /><p class=\"spinner\"></p><p class=\"img\"><h3 id=\"" + friend_id + "\">" + name + "</h3></p><p class=\"info\"><p class=\"info-back\"><h3 class=\"info-word\">" + info_word + "</h3></p></p></a></p></p>"; //向数据库添加,通过结果来确定界面显示 $.ajax({ url: "userlist.aspx/AddFriend", data: "{'username':'" + $('#username').text() + "','friend_id':'" + friend_id + "'}", type: 'Post', contentType: "application/json; charset=utf-8", dataType: "json", success: function (result) { if (result.d == "true") { $(".case-content").append(content);//数据库添加成功,插入html代码 } else { alert(result.d); } }, error: function (err) { alert("未知错误"); } }); } //删除好友 function Delete(e) { var friend_id = e.id; $.ajax({ url: "userlist.aspx/DeleteFriend", data: "{'username':'" + $('#username').text() + "','friend_id':'" + friend_id + "'}", type: 'Post', contentType: "application/json; charset=utf-8", dataType: "json", success: function (result) { if (result.d == true) { $("#" + friend_id).remove();//在界面移除好友 } if (result.d == false) { alert("删除失败"); } }, error: function (err) { alert("未知错误"); } }); } //重置好友-切换到删除模式 function ChangeToDelete() { $(".case-item").removeAttr("onclick");//删除onclick事件 $(".case-item").attr("onclick", "Delete(this);");//添加新的onclick事件 $(".img_wrong").css("display", "block");//使删除图标可见 $(".info-word").html("删除");//改变提示文字 } //关闭重置-切换到正常模式 function ChangeToNormal() { $(".case-item").removeAttr("onclick");//删除onclick事件 $(".case-item").attr("onclick", "");//添加新的onclick事件 $(".img_wrong").css("display", "none");//使删除图标不可见 $(".info-word").html(info_back);//恢复提示文字 }
There are two points learned in this exercise:
1. Interaction between Ajax and the background;
2. JQuery’s attribute control of page elements
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
jQuery operates the list soldier to dynamically add new elements to it
jQuery adds an assignment step to the child elements Detailed explanation
Detailed explanation on the use of jQuery Magnify plug-in
The above is the detailed content of How jQuery dynamically controls page elements. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

"Methods to handle Laravel pages that cannot display CSS correctly, need specific code examples" When using the Laravel framework to develop web applications, sometimes you will encounter the problem that the page cannot display CSS styles correctly, which may cause the page to render abnormal styles. Affect user experience. This article will introduce some methods to deal with the failure of Laravel pages to display CSS correctly, and provide specific code examples to help developers solve this common problem. 1. Check the file path. First check the path of the CSS file.

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Implementation method of page jump in 3 seconds: PHP Programming Guide In web development, page jump is a common operation. Generally, we use meta tags in HTML or JavaScript methods to jump to pages. However, in some specific cases, we need to perform page jumps on the server side. This article will introduce how to use PHP programming to implement a function that automatically jumps to a specified page within 3 seconds, and will also give specific code examples. The basic principle of page jump using PHP. PHP is a kind of

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute
