jQuery PHP creates sliding switch effect_jquery
This article introduces the use of jQuery, PHP and MySQL to implement a switch similar to the 360 Security Guard firewall on and off. This function can be applied to the on and off functions of product functions.
Preparation work In order to better demonstrate this example, we need a data table to record the required function description and opening status. The table structure is as follows:
<div class="codetitle">
<span><a style="CURSOR: pointer" data="61350" class="copybut" id="copybut61350" onclick="doCopy('code61350')"><u>复制代码</u></a></span> 代码如下:</div>
<div class="codebody" id="code61350">
<br>
CREATE TABLE `pro` ( <br>
`id` int(11) NOT NULL auto_increment, <br>
`title` varchar(50) NOT NULL, <br>
`description` varchar(200) NOT NULL, <br>
`status` tinyint(1) NOT NULL default '0', <br>
PRIMARY KEY (`id`) <br>
) ENGINE=MyISAM DEFAULT CHARSET=utf8;<br>
</div>
You can pro insert several pieces of data into the table.
index.php
We want to display a list of related functions on the page, use PHP to read the data table, and display it in the form of a list.
<div class="codetitle">
<span><a style="CURSOR: pointer" data="53064" class="copybut" id="copybut53064" onclick="doCopy('code53064')"><u>复制代码</u></a></span> 代码如下:</div>
<div class="codebody" id="code53064">
<br>
<?php <br />
require_once('connect.php'); //连接数据库 <br />
$query=mysql_query("select * from pro order by id asc"); <br />
while ($row=mysql_fetch_array($query)) { <br />
?> <br>
<div class="list"> <br>
<div class="fun_title"> <br>
<span rel="<?php echo $row['id'];?>" <?php if($row['status']==1){ ?> <br>
class="ad_on" title="点击关闭"<?php }else{?>class="ad_off" title="点击开启"<?php }?>></span> <br>
<h3><?php echo $row['title']; ?></h3> <br>
</div> <br>
<p><?php echo $row['description'];?></p> <br>
</div> <br>
<?php } ?><br>
</div>
Connect to the database and then loop to output the product function list.
CSS
In order to render a better page appearance, we use CSS to beautify the page and make it more user-friendly. Using CSS, we only need an image to identify the switch button.
<div class="codetitle">
<span><a style="CURSOR: pointer" data="31038" class="copybut" id="copybut31038" onclick="doCopy('code31038')"><u>复制代码</u></a></span> 代码如下:</div>
<div class="codebody" id="code31038">
<br>
.list{padding:6px 4px; border-bottom:1px dotted #d3d3d3; position:relative} <br>
.fun_title{height:28px; line-height:28px} <br>
.fun_title span{width:82px; height:25px; background:url(switch.gif) no-repeat; <br>
cursor:pointer; position:absolute; right:6px; top:16px} <br>
.fun_title span.ad_on{background-position:0 -2px} <br>
.fun_title span.ad_off{background-position:0 -38px} <br>
.fun_title h3{font-size:14px; font-family:'microsoft yahei';} <br>
.list p{line-height:20px} <br>
.list p span{color:#f60} <br>
.cur_select{background:#ffc}<br>
</div>
I don’t want to go into details about the CSS code. As a reminder, we use an image and then use background-position to position the image. This is the method used by most websites. I won’t go into the benefits.
jQuery
By clicking the switch button, we request the background in time to change the corresponding function switch status. This process is a typical Ajax application. By clicking the switch button, the front-end sends a post request to the background PHP, the background receives the request, queries the database, and returns the results to the front-end. The front-end jQuery changes the button state based on the results returned by the background.
<div class="codetitle">
<span><a style="CURSOR: pointer" data="70559" class="copybut" id="copybut70559" onclick="doCopy('code70559')"><u>Copy code</u></a></span> The code is as follows:</div>
<div class="codebody" id="code70559">
<br>
$(function(){ <br>
//鼠标滑向换色 <br>
$(".list").hover(function(){ <br>
$(this).addClass("cur_select"); <br>
},function(){ <br>
$(this).removeClass("cur_select"); <br>
}); <br>
//关闭 <br>
$(".ad_on").live("click",function(){ <br>
var add_on = $(this); <br>
var status_id = $(this).attr("rel"); <br>
$.post("action.php",{status:status_id,type:1},function(data){ <br>
if(data==1){ <br>
add_on.removeClass("ad_on").addClass("ad_off").attr("title","点击开启"); <br>
}else{ <br>
alert(data); <br>
} <br>
}); <br>
}); <br>
//开启 <br>
$(".ad_off").live("click",function(){ <br>
var add_off = $(this); <br>
var status_id = $(this).attr("rel"); <br>
$.post("action.php",{status:status_id,type:2},function(data){alert(data); <br>
if(data==1){ <br>
add_off.removeClass("ad_off").addClass("ad_on").attr("title","点击关闭"); <br>
}else{ <br>
alert(data); <br>
} <br>
}); <br>
}); <br>
});<br>
</div>
说明,代码中,首先实现了鼠标滑向功能列表换色的功能(详见demo),然后就是单击开关按钮,向后台action.php发送Ajax请求,提交的参数是对应功能的id和type,用于后台区分请求的是哪个功能和请求的类型(开启和关闭)。其实,大家稍微留神,可以看出,根据Ajax请求成功返回结果后,开关按钮动态改变样式,实现改变开关状态的功能。
action.php
后台action.php接收到前端的请求,根据参数执行SQL语句,更新对应功能的状态,成功后将结果返回给前端,请看代码:
<div class="codetitle">
<span><a style="CURSOR: pointer" data="51996" class="copybut" id="copybut51996" onclick="doCopy('code51996')"><u>复制代码</u></a></span> 代码如下:<code><div class="codetitle">
<span><a style="CURSOR: pointer" data="51996" class="copybut" id="copybut51996" onclick="doCopy('code51996')"><u>复制代码</u></a></span> 代码如下:</div>
<div class="codebody" id="code51996">
<br>
require_once('connect.php'); <br>
$id = $_POST['status']; <br>
$type = $_POST['type']; <br>
if($type==1){ //关闭 <br>
$sql = "update pro set status=0 where id=".$id; <br>
}else{ //开启 <br>
$sql = "update pro set status=1 where id=".$id; <br>
} <br>
$rs = mysql_query($sql); <br>
if($rs){ <br>
echo '1'; <br>
}else{ <br>
echo '服务器忙,请稍后再试!'; <br>
}<br>
</div>
$id = $_POST['status'];
$type = $_POST['type']; if($type==1){ //关闭 $sql = "update pro set status=0 where id=".$id; }else{ //开启 $sql = "update pro set status=1 where id=".$id; } $rs = mysql_query($sql); if($rs){ echo '1'; }else{ echo '服务器忙,请稍后再试!'; } 结束语通过本文您可以熟练掌握ajax在WEB开发中的应用,并能快速的应用到您的项目中。将一如既往的为广大开发者提供更具实用性的应用,致力于WEB前端技术的应用。
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

Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

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

jQuery is a fast, small, feature-rich JavaScript library widely used in front-end development. Since its release in 2006, jQuery has become one of the tools of choice for many developers, but in practical applications, it also has some advantages and disadvantages. This article will deeply analyze the advantages and disadvantages of jQuery and illustrate it with specific code examples. Advantages: 1. Concise syntax jQuery's syntax design is concise and clear, which can greatly improve the readability and writing efficiency of the code. for example,

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: 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: 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
