php页面按钮效果代码_jQuery+PHP打造滑动开关效果

https://blog.csdn.net/weixin_39662462/article/details/115105302

但是源代码有点问题,以下做了修正

本文介绍了使用jQuery、PHP和MySQL实现类似360安全卫士防火墙开启关闭的开关,可以将此功能应用在产品功能的开启和关闭功能上。

准备工作为了更好的演示本例,我们需要一个数据表,记录需要的功能说明及开启状态,表结构如下:

复制代码代码如下:

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`) 
) DEFAULT CHARSET=utf8;

  

 你可以向表中pro插入几条数据。

INSERT INTO `pro1111` (`id`, `title`, `description`, `status`) VALUES ('9', 'tttt999', 'sggshtrht', '1');
INSERT INTO `pro1111` (`id`, `title`, `description`, `status`) VALUES ('10', 'tttt10', 'sggshtrht', '2');
INSERT INTO `pro1111` (`id`, `title`, `description`, `status`) VALUES ('11', 'tttt11', 'sggshtrht', '1');
INSERT INTO `pro1111` (`id`, `title`, `description`, `status`) VALUES ('12', 'tttt12', 'sggshtrht', '0');

  

switchbutton.php

我们要在页面显示相关功能列表,使用PHP读取数据表,并以列表的形式展示。

复制代码代码如下:
 
<!-- index.php -->
<html>

<head>
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <link rel="stylesheet" type="text/css" href="./css/switchbutton.css">
  <script src="./js/jquery-3.6.0.min.js"></script>>
  <script src="./js/switchbutton.js"></script>>
</head>

</html>

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("连接失败: " . $conn->connect_error);
}

$sql = "SELECT * FROM `pro1111`";
$result = $conn->query($sql);

// 输出数据
while ($row = $result->fetch_assoc()) {
?>
  <div class="list">
    <div class="fun_title">
      <span rel="<?php echo $row['id']; ?>" <?php
                                            if ($row['status'] == 1) {
                                              echo "class=\"ad_on\" title=\"点击关闭\"";
                                            } else {
                                              echo "class=\"ad_off\" title=\"点击开启\"";
                                            }
                                            ?>></span>

      <h3><?php echo $row['title']; ?></h3>
      </>
      <p><?php echo $row['description']; ?></p>
    </div>
  </div>
<?php } ?>

  

连接数据库,然后循环输出产品功能列表。

《 ./css/switchbutton.css 

为了渲染一个比较好的页面外观,我们使用CSS来美化页面,使得页面更符合人性化。使用CSS,我们只需用一张图片来标识开关按钮。

 

复制代码代码如下:

.list{padding:6px 4px; border-bottom:1px dotted #d3d3d3; position:relative}   
.fun_title{height:28px; line-height:28px}   
/* .fun_title span{82px; height:25px; background:url(switch.gif) no-repeat;     */
  .fun_title span{82px; height:25px; background:#f00 url(../image/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来定位图片的位置,这是大多数网站使用的方法,好处咱就不说了。

jQuery

我们通过单击开关按钮,及时请求后台,改变对应的功能开关状态。这个过程是一个典型的Ajax应用。通过点击开关按钮,前端向后台PHP发送post请求,后台接收请求,并查询数据库,并将结果返回给前端,前端jQuery根据后台返回的结果,改变按钮状态。

复制代码代码如下:
注意:jquery的事件绑定在页面加载时就已经完成,所以之后动态添加的class将无法绑上事件。, 所以使用live可以解决这个问题。不过live只支持jquery1.9以前(大概),
所以这里改为使用 采用事件委托或代理方式绑定。
demo:
$(document).on("click", ".remove", function(e){
     console.log(e);
     $(this).parent().remove()
 });

《switchbutton.js》 

所以 代码如下

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

  //关闭
  $(document).on("click", ".ad_on", function (e) {
    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);
      }
    });

    // $(this).parent().remove()
  });

  //开启   
  $(document).on("click", ".ad_off", function (e) {
    var add_off = $(this);
    var status_id = $(this).attr("rel");

    $.post("action.php", { status: status_id, type: 2 }, function (data) {
      if (data == 1) {
        add_off.removeClass("ad_off").addClass("ad_on").attr("title", "点击关闭");
      } else {
        alert(data);
      }
    });

    // $(this).parent().remove()
  });
});

  

说明,代码中,首先实现了鼠标滑向功能列表换色的功能(详见demo),然后就是单击开关按钮,向后台action.php发送Ajax请求,提交的参数是对应功能的id和type,用于后台区分请求的是哪个功能和请求的类型(开启和关闭)。其实,大家稍微留神,可以看出,根据Ajax请求成功返回结果后,开关按钮动态改变样式,实现改变开关状态的功能。

action.php

后台action.php接收到前端的请求,根据参数执行SQL语句,更新对应功能的状态,成功后将结果返回给前端,请看代码:

复制代码代码如下:
<?php

// require_once('connect.php');  
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("连接失败: " . $conn->connect_error);
}

$sql = "SELECT * FROM `pro1111`";
$result = $conn->query($sql);

// action
$id = $_POST['status'];
$type = $_POST['type'];

if ($type == 1) { //关闭
  $sql = "update pro1111 set status=0 wh

结束语通过本文您可以熟练掌握ajax在WEB开发中的应用,并能快速的应用到您的项目中。将一如既往的为广大开发者提供更具实用性的应用,致力于WEB前端技术的应用。

原文地址:https://www.cnblogs.com/carl2380/p/15699382.html