jQuery实现简单的Web备忘录

实现功能:

  1. 添加备忘事件
  2. 自动获取备忘时间
  3. 在未完成事件中点击对号,相应的事件会走到已完成事件中
  4. 在已完成事件中点击对号,相应的事件会消失

实现效果:
在这里插入图片描述
index.html文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>备忘录</title>
    <link rel="stylesheet" href="css/index.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">
    <script src="js/jQuery.min.js"></script>
    <script src="js/index.js"></script>
</head>
<body>
    <div class="header">
        <input type="text">
        <button class="btn btn-success">添加备忘</button>
    </div>
    <div class="con">
        <div class="weiwancheng">
            <p>未完成</p>
            <ul>
                
            </ul>
        </div>
        
        <div class="yiwancheng">
            <p>已完成</p>
            <ul>
               
            </ul>
        </div>
        
    </div>
</body>
</html>

index.css文件:

* {
    margin: 0;
    padding: 0;
    outline: none!important;
}
html {
    height: 100%;
}
body {
     height: 100%;
     background-image: -webkit-linear-gradient(left,#fbc2eb, #a6c1ee);
}
.header {
     width: 500px;
     height: 42px;
     margin: 10px auto;
     text-align: center;
}
.header input {
    width: 300px;
    height: 40px;
    border: 0;
    border-radius: 10px;
    padding: 0 10px;
}
.header button {
    height: 40px;
    margin-left: 10px;
    border: 0;
}
.weiwancheng,
.yiwancheng {
    width: 600px;
    margin: 10px auto;
}
.weiwancheng ul,
.yiwancheng ul {
    width: 100%;
}
.weiwancheng ul li,
.yiwancheng ul li {
    display: flex;
    align-items: center;
    list-style: none;
    line-height: 30px;
    width: 100%;
    margin-top: 10px;
    border: 0;
    border-radius: 10px;
    background-color: aqua;
}
.content {
    display: inline-block;
    width: 400px;
    word-wrap:break-word;
    margin: 0!important;
    padding: 0 10px;
}
.weiwancheng ul li em,
.yiwancheng ul li em {
    margin-left: 20px;
    cursor: pointer;
    height: 14px;
    width: 14px;
}

index.js:

$(function () {
    $(".btn").click(function () {
        if ($(".header input").val() != '') {
            var li = $("<li></li>");
            $(".weiwancheng ul").prepend(li);
            var p = $("<p></p>");
            p.addClass("content");
            li.append(p);
            p.text($(".header input").val());
            function getNow(s) {
                return s < 10 ? '0' + s : s;
            }

            var myDate = new Date();

            var year = myDate.getFullYear();        //获取当前年
            var month = myDate.getMonth() + 1;   //获取当前月
            var date = myDate.getDate();            //获取当前日
            var h = myDate.getHours();              //获取当前小时数(0-23)
            var m = myDate.getMinutes();          //获取当前分钟数(0-59)
            var s = myDate.getSeconds();

            var now = year + '-' + getNow(month) + "-" + getNow(date) + " " + getNow(h) + ':' + getNow(m) + ":" + getNow(s);
            var span = $("<span></span>");
            li.append(span);
            span.text(now);
            var em = $("<em></em>");
            em.addClass("glyphicon glyphicon-ok");
            li.append(em);
           
            $(em).click(function () {
                var li = $("<li></li>");
                $(".yiwancheng ul").prepend(li);
                var p = $("<p></p>");
                p.addClass("content");
                li.append(p);
                p.text($(this).siblings("p").text());
                function getNow(s) {
                    return s < 10 ? '0' + s : s;
                }

                var myDate = new Date();

                var year = myDate.getFullYear();        //获取当前年
                var month = myDate.getMonth() + 1;   //获取当前月
                var date = myDate.getDate();            //获取当前日
                var h = myDate.getHours();              //获取当前小时数(0-23)
                var m = myDate.getMinutes();          //获取当前分钟数(0-59)
                var s = myDate.getSeconds();

                var now = year + '-' + getNow(month) + "-" + getNow(date) + " " + getNow(h) + ':' + getNow(m) + ":" + getNow(s);
                var span = $("<span></span>");
                li.append(span);
                span.text(now);
                var em = $("<em></em>");
                em.addClass("glyphicon glyphicon-ok");
                li.append(em);
                $(this).parent().remove();
                $(".yiwancheng ul li em").click(function () {
                    $(this).parent().remove();
                });
            });
            
        } else {
            alert('备忘不能为空!');
        }
    });
})
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. HTML代码 ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>备忘录</title> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> </head> <body> <h2>备忘录</h2> <form> <label>标题:</label> <input type="text" id="title"><br><br> <label>内容:</label> <textarea id="content"></textarea><br><br> <input type="button" value="添加备忘录" onclick="addMemo()"> </form> <hr> <h3>备忘录列表</h3> <ul id="memoList"> </ul> <script src="memo.js"></script> </body> </html> ``` 2. JavaScript代码 ```javascript // 添加备忘录 function addMemo() { var title = $("#title").val(); var content = $("#content").val(); $.ajax({ type: "POST", url: "add_memo.php", // 处理新增备忘录的PHP文件 data: {title: title, content: content}, dataType: "json", success: function(data){ if(data.status == "success"){ alert("添加成功!"); $("#title").val(""); $("#content").val(""); getMemoList(); // 添加成功后,重新获取备忘录列表 }else{ alert("添加失败!"); } } }); } // 获取备忘录列表 function getMemoList() { $.ajax({ type: "GET", url: "memo_list.php", // 处理获取备忘录列表的PHP文件 dataType: "json", success: function(data){ if(data.status == "success"){ var memoListHtml = ""; for(var i=0; i<data.memoList.length; i++){ memoListHtml += "<li><h4>"+data.memoList[i].title+"</h4><p>"+data.memoList[i].content+"</p></li>"; } $("#memoList").html(memoListHtml); }else{ alert("获取备忘录列表失败!"); } } }); } $(function(){ getMemoList(); // 页面加载时获取备忘录列表 }); ``` 3. PHP代码(add_memo.php) ```php <?php header('Content-Type: application/json; charset=utf-8'); // 获取POST数据 $title = $_POST["title"]; $content = $_POST["content"]; // 连接数据库 $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "memo"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // SQL语句 $sql = "INSERT INTO memo (title, content) VALUES ('$title', '$content')"; if ($conn->query($sql) === TRUE) { $result = array("status"=>"success"); } else { $result = array("status"=>"fail"); } $conn->close(); echo json_encode($result); ?> ``` 4. PHP代码(memo_list.php) ```php <?php header('Content-Type: application/json; charset=utf-8'); // 连接数据库 $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "memo"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // SQL语句 $sql = "SELECT * FROM memo ORDER BY id DESC"; $result = $conn->query($sql); if ($result->num_rows > 0) { $memoList = array(); while($row = $result->fetch_assoc()) { $memo = array( "title"=>$row["title"], "content"=>$row["content"] ); array_push($memoList, $memo); } $result = array("status"=>"success", "memoList"=>$memoList); } else { $result = array("status"=>"fail"); } $conn->close(); echo json_encode($result); ?> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值