人人人妻人人人妻人人人,99精品国产综合久久久久五月天 ,欧美白人最猛性XXXXX,日韩AV无码免费播放

News新聞

業(yè)界新聞動態(tài)、技術(shù)前沿
Who are we?

您的位置:首頁      JS/JQ/AJAX      使用jQuery和PHP實現(xiàn)類似360功能開關(guān)效果

使用jQuery和PHP實現(xiàn)類似360功能開關(guān)效果

標簽: 發(fā)布日期:2014-03-31 00:00:00 442

本文介紹了使用jQuery、PHP和MySQL實現(xiàn)類似360安全衛(wèi)士防火墻開啟關(guān)閉的開關(guān),可以將此功能應(yīng)用在產(chǎn)品功能的開啟和關(guān)閉功能上,需要的朋友可以參考下

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

 

復制代碼代碼如下:

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

 

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

index.php

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

 

復制代碼代碼如下:

<?php   
   require_once('connect.php'); //連接數(shù)據(jù)庫   
   $query=mysql_query("select * from pro order by id asc");   
   while ($row=mysql_fetch_array($query)) {   
   ?>   
   <div class="list">   
     <div class="fun_title">   
        <span rel="<?php echo $row['id'];?>" <?php if($row['status']==1){ ?>   
class="ad_on" title="點擊關(guān)閉"<?php }else{?>class="ad_off" title="點擊開啟"<?php }?>></span>   
        <h3><?php echo $row['title']; ?></h3>   
     </div>   
     <p><?php echo $row['description'];?></p>   
   </div>   
 <?php } ?>

 

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

css

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

 

復制代碼代碼如下:

.list{padding:6px 4px; border-bottom:1px dotted #d3d3d3; position:relative}   
.fun_title{height:28px; line-height:28px}   
.fun_title span{width:82px; height:25px; background:url(switch.gif) no-repeat;    
cursor:pointer; position:absolute; right:6px; top:16px}   
.fun_title span.ad_on{background-position:0 -2px}   
.fun_title span.ad_off{background-position:0 -38px}   
.fun_title h3{font-size:14px; font-family:'microsoft yahei';}   
.list p{line-height:20px}   
.list p span{color:#f60}   
.cur_select{background:#ffc}

 

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

jQuery

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

 

復制代碼代碼如下:

$(function(){   
    //鼠標滑向換色   
    $(".list").hover(function(){   
        $(this).addClass("cur_select");   
    },function(){   
        $(this).removeClass("cur_select");   
    });   

    //關(guān)閉   
    $(".ad_on").live("click",function(){   
        var add_on = $(this);   
        var status_id = $(this).attr("rel");   
        $.post("action.php",{status:status_id,type:1},function(data){   
            if(data==1){   
                add_on.removeClass("ad_on").addClass("ad_off").attr("title","點擊開啟");   
            }else{   
                alert(data);   
            }   
        });   
    });   
    //開啟   
    $(".ad_off").live("click",function(){   
        var add_off = $(this);   
        var status_id = $(this).attr("rel");   
        $.post("action.php",{status:status_id,type:2},function(data){alert(data);     
            if(data==1){   
                add_off.removeClass("ad_off").addClass("ad_on").attr("title","點擊關(guān)閉");   
            }else{   
                alert(data);   
            }   
        });   
    });   
});

 

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

action.php

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

 

復制代碼代碼如下:

require_once('connect.php');   
$id = $_POST['status'];   
$type = $_POST['type'];   
if($type==1){ //關(guān)閉   
    $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 '服務(wù)器忙,請稍后再試!';   
}