偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

jQuery和PHP打造功能開關(guān)效果

開發(fā) 后端
本文以360安全衛(wèi)士的木馬防火墻開關(guān)為背景,使用PHP、jquery、MYSQL實(shí)現(xiàn)了及時(shí)開啟和關(guān)閉產(chǎn)品功能的WEB應(yīng)用。為了更好的演示本例,我們需要一個(gè)數(shù)據(jù)表,記錄需要的功能說(shuō)明及開啟狀態(tài),表結(jié)構(gòu)如下。

 

在開發(fā)項(xiàng)目中,我們會(huì)經(jīng)常碰到需要及時(shí)開啟某項(xiàng)功能的情況,通過(guò)Ajax實(shí)現(xiàn)實(shí)時(shí)開啟和關(guān)閉功能,無(wú)疑增強(qiáng)了用戶體驗(yàn)。本文以360安全衛(wèi)士的木馬防火墻開關(guān)為背景,使用PHP、jquery、MYSQL實(shí)現(xiàn)了及時(shí)開啟和關(guān)閉產(chǎn)品功能的WEB應(yīng)用。

查看演示DEMO

準(zhǔn)備工作

為了更好的演示本例,我們需要一個(gè)數(shù)據(jù)表,記錄需要的功能說(shuō)明及開啟狀態(tài),表結(jié)構(gòu)如下:

  1. CREATE TABLE `pro` (  
  2.   `id` int(11) NOT NULL auto_increment,  
  3.   `title` varchar(50) NOT NULL,  
  4.   `description` varchar(200) NOT NULL,  
  5.   `status` tinyint(1) NOT NULL default '0',  
  6.   PRIMARY KEY  (`id`)  
  7. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;  

你可以向表中pro插入幾條數(shù)據(jù)。

index.php

我們要在頁(yè)面顯示相關(guān)功能列表,使用PHP讀取數(shù)據(jù)表,并以列表的形式展示。

  1. <?php   
  2.    require_once('connect.php'); //連接數(shù)據(jù)庫(kù)   
  3.    $query=mysql_query("select * from pro order by id asc");   
  4.    while ($row=mysql_fetch_array($query)) {   
  5.    ?>   
  6.    < class="list">   
  7.      < class="fun_title">   
  8.         <span rel="<?php echo $row['id'];?>" <?php if($row['status']==1){ ?>   
  9. class="ad_on" title="點(diǎn)擊關(guān)閉"<?php }else{?>class="ad_off" title="點(diǎn)擊開啟"<?php }?>></span>   
  10.         <h3><?php echo $row['title']; ?></h3>   
  11.      </>   
  12.      <p><?php echo $row['description'];?></p>   
  13.    </>   
  14.  <?php } ?>   

連接數(shù)據(jù)庫(kù),然后循環(huán)輸出產(chǎn)品功能列表。

CSS

為了渲染一個(gè)比較好的頁(yè)面外觀,我們使用CSS來(lái)美化頁(yè)面,使得頁(yè)面更符合人性化。使用CSS,我們只需用一張圖片來(lái)標(biāo)識(shí)開關(guān)按鈕。

jquery開關(guān)

  1. .list{padding:6px 4pxborder-bottom:1px dotted #d3d3d3position:relative}   
  2. .fun_title{height:28pxline-height:28px}   
  3. .fun_title span{width:82pxheight:25pxbackground:url(switch.gif) no-repeat;    
  4. cursor:pointerposition:absolute; right:6px; top:16px}   
  5. .fun_title span.ad_on{background-position:0 -2px}   
  6. .fun_title span.ad_off{background-position:0 -38px}   
  7. .fun_title h3{font-size:14pxfont-family:'microsoft yahei';}   
  8. .list p{line-height:20px}   
  9. .list p span{color:#f60}   
  10. .cur_select{background:#ffc}   

CSS代碼,我不想詳述,提示下我們使用了一張圖片,然后通過(guò)background-position來(lái)定位圖片的位置,這是大多數(shù)網(wǎng)站使用的方法,好處咱就不說(shuō)了。

jQuery

我們通過(guò)單擊開關(guān)按鈕,及時(shí)請(qǐng)求后臺(tái),改變對(duì)應(yīng)的功能開關(guān)狀態(tài)。這個(gè)過(guò)程是一個(gè)典型的Ajax應(yīng)用。通過(guò)點(diǎn)擊開關(guān)按鈕,前端向后臺(tái)PHP發(fā)送post請(qǐng)求,后臺(tái)接收請(qǐng)求,并查詢數(shù)據(jù)庫(kù),并將結(jié)果返回給前端,前端jQuery根據(jù)后臺(tái)返回的結(jié)果,改變按鈕狀態(tài)。

  1. $(function(){   
  2.     //鼠標(biāo)滑向換色   
  3.     $(".list").hover(function(){   
  4.         $(this).addClass("cur_select");   
  5.     },function(){   
  6.         $(this).removeClass("cur_select");   
  7.     });   
  8.        
  9.     //關(guān)閉   
  10.     $(".ad_on").live("click",function(){   
  11.         var add_on = $(this);   
  12.         var status_id = $(this).attr("rel");   
  13.         $.post("action.php",{status:status_id,type:1},function(data){   
  14.             if(data==1){   
  15.                 add_on.removeClass("ad_on").addClass("ad_off").attr("title","點(diǎn)擊開啟");   
  16.             }else{   
  17.                 alert(data);   
  18.             }   
  19.         });   
  20.     });   
  21.     //開啟   
  22.     $(".ad_off").live("click",function(){   
  23.         var add_off = $(this);   
  24.         var status_id = $(this).attr("rel");   
  25.         $.post("action.php",{status:status_id,type:2},function(data){alert(data);     
  26.             if(data==1){   
  27.                 add_off.removeClass("ad_off").addClass("ad_on").attr("title","點(diǎn)擊關(guān)閉");   
  28.             }else{   
  29.                 alert(data);   
  30.             }   
  31.         });   
  32.     });   
  33. });   

說(shuō)明,代碼中,首先實(shí)現(xiàn)了鼠標(biāo)滑向功能列表?yè)Q色的功能(詳見(jiàn)demo),然后就是單擊開關(guān)按鈕,向后臺(tái)action.php發(fā)送Ajax請(qǐng)求,提交的參數(shù)是對(duì)應(yīng)功能的id和type,用于后臺(tái)區(qū)分請(qǐng)求的是哪個(gè)功能和請(qǐng)求的類型(開啟和關(guān)閉)。其實(shí),大家稍微留神,可以看出,根據(jù)Ajax請(qǐng)求成功返回結(jié)果后,開關(guān)按鈕動(dòng)態(tài)改變樣式,實(shí)現(xiàn)改變開關(guān)狀態(tài)的功能。

action.php

后臺(tái)action.php接收到前端的請(qǐng)求,根據(jù)參數(shù)執(zhí)行SQL語(yǔ)句,更新對(duì)應(yīng)功能的狀態(tài),成功后將結(jié)果返回給前端,請(qǐng)看代碼:

  1. require_once('connect.php');   
  2. $id = $_POST['status'];   
  3. $type = $_POST['type'];   
  4. if($type==1){ //關(guān)閉   
  5.     $sql = "update pro set status=0 where id=".$id;   
  6. }else//開啟   
  7.     $sql = "update pro set status=1 where id=".$id;   
  8. }   
  9. $rs = mysql_query($sql);   
  10. if($rs){   
  11.     echo '1';   
  12. }else{   
  13.     echo '服務(wù)器忙,請(qǐng)稍后再試!';   
  14. }   


原文鏈接:http://www.helloweba.com/view-blog-153.html

責(zé)任編輯:張偉 來(lái)源: helloweba
相關(guān)推薦

2012-02-08 17:01:36

2025-06-16 02:00:00

開發(fā)部署功能開關(guān)

2010-12-28 13:44:12

PHPXMLjQuery

2012-03-07 14:37:03

JavaJavaMail

2012-11-05 10:36:40

IBMdw

2010-05-31 09:19:53

PHP

2012-08-22 10:28:03

jQuery

2023-04-26 09:37:25

智駕開發(fā)

2021-11-02 10:10:49

鴻蒙HarmonyOS應(yīng)用

2023-02-28 15:49:09

鴻蒙應(yīng)用開發(fā)

2012-06-13 15:21:26

jQuery

2017-12-11 14:50:34

前端Javascript文本朗讀

2012-04-26 10:56:05

jQuery效果

2012-01-10 14:59:42

jQuery

2009-10-14 08:32:14

Windows 7PHP高校IT

2011-11-11 14:05:13

Jscex

2010-12-22 09:56:24

PHP

2009-11-30 14:27:42

2011-07-07 14:14:41

PHP模版

2012-05-30 10:43:47

jQuery
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)