jQuery实现留言板

效果展示:

目录

         HTML代码

         JS代码

         CSS代码


HTML代码

<body>
    <div id="msgBox">
        <form>
            <h2>来 , 说说你在做什么 , 想什么</h2>
            <div>
                <input id="userName" class="f-text" value="" />
                <p id="face">
                    <img src="img/face1.gif" class="current active" />
                    <img src="img/face2.gif" />
                    <img src="img/face3.gif" />
                    <img src="img/face4.gif" />
                    <img src="img/face5.gif" />
                    <img src="img/face6.gif" />
                </p>
            </div>
            <div><textarea id="conBox" class="f-text" maxlength="140"></textarea></div>
            <div class="tr">
                <p>
                    <span class="countTxt">还能输入</span><strong class="maxNum">140</strong><span>个字</span>
                    <input id="sendBtn" type="button" value="" title="快捷键 Ctrl+Enter" />
                </p>
            </div>
        </form>
        <div class="list">
            <h3><span>大家在说</span></h3>
            <ul>
                <li>
                    <div class="userPic"><img src="img/face.gif" /></div>
                    <div class="content">
                        <div class="userName"><a href="javascript:;">永不上线</a>:</div>
                        <div class="msgInfo">新增了好多你不知道的功能哦!</div>
                        <div class="times"><span>01月18日 18:22</span><a class="del" href="javascript:;">删除</a></div>
                    </div>
                </li>
            </ul>
        </div>
    </div>
</body>

JS代码

<script src="../jquery/jquery.js"></script>
<script>
    let src = ''
    $('#face>img').on('mouseover', function () {
        $(this).addClass('active').siblings('img').removeClass('active')
    })
    $('#fase>img').on('mouseout', function () {
        $(this).removeClass('active')
    })
    $('#face>img').on('click', function () {
        $(this).addClass('current').siblings('img').removeClass('current')
        src = $(this).prop('src');
    })
    $('textarea').on('keyup', function () {
        let num = (140 - $(this).val().length)
        $('.maxNum').html(num)
    })
    $('#sendBtn').on('click', function () {
        console.log(1);
        let $li = $('<li></li>')
        $li.html(`<div class="userPic"><img src="${src}" /></div>
                    <div class="content">
                        <div class="userName"><a href="javascript:;">${$('#userName').val()}</a>:</div>
                        <div class="msgInfo">${$('#conBox').val()}</div>
                        <div class="times"><span>${getDay()}</span><a class="del" href="javascript:;">删除</a></div>
                    </div>`)
        $('.list>ul').append($li)
    })
</script>
function getDay(){
    let date = new Date();
    let m = date.getMonth()+1;
    let d = date.getDate();
    let h = date.getHours();
    let f = date.getMinutes();
    h = h<10?'0'+h:h;
    f = f<10?'0'+f:f;
    return m+'月'+d+'日'+" "+h+":"+f;
}

CSS代码

    <style type="text/css">
        body,
        div,
        h2,
        h3,
        ul,
        li,
        p {
            margin: 0;
            padding: 0;
        }
        a {
            text-decoration: none;
        }
        a:hover {
            text-decoration: underline;
        }
        ul {
            list-style-type: none;
        }
        body {
            color: #333;
            background-color: lightblue;
            font: 12px/1.5 \5b8b\4f53;
        }
        #msgBox {
            width: 500px;
            background: #fff;
            border-radius: 5px;
            margin: 10px auto;
            padding-top: 10px;
        }
        #msgBox form h2 {
            font-weight: 400;
            font: 400 18px/1.5 \5fae\8f6f\96c5\9ed1;
        }
        #msgBox form {
            background: url(img/boxBG.jpg) repeat-x 0 bottom;
            padding: 0 20px 15px;
        }
        #userName,
        #conBox {
            color: #777;
            border: 1px solid #d0d0d0;
            border-radius: 6px;
            background: #fff url(img/inputBG.png) repeat-x;
            padding: 3px 5px;
            font: 14px/1.5 arial;
        }
        #userName.active,
        #conBox.active {
            border: 1px solid #7abb2c;
        }
        #userName {
            height: 20px;
        }
        #conBox {
            width: 448px;
            resize: none;
            height: 65px;
            overflow: auto;
        }
        #msgBox form div {
            position: relative;
            color: #999;
            margin-top: 10px;
        }
        #msgBox img {
            border-radius: 3px;
        }
        #face {
            position: absolute;
            top: 0;
            left: 172px;
        }
        #face img {
            width: 30px;
            height: 30px;
            cursor: pointer;
            margin-right: 6px;
            opacity: 0.5;
            filter: alpha(opacity=50);
        }
        #face img.hover,
        #face img.current,
        #face img.active {
            width: 28px;
            height: 28px;
            border: 1px solid #f60;
            opacity: 1;
            filter: alpha(opacity=100);
        }
        #sendBtn {
            border: 0;
            width: 112px;
            height: 30px;
            cursor: pointer;
            margin-left: 10px;
            background: url(img/btn.png) no-repeat;
        }
        #sendBtn.hover {
            background-position: 0 -30px;
        }
        #msgBox form .maxNum {
            font: 26px/30px Georgia, Tahoma, Arial;
            padding: 0 5px;
        }
        #msgBox .list {
            padding: 10px;
        }
        #msgBox .list h3 {
            position: relative;
            height: 33px;
            font-size: 14px;
            font-weight: 400;
            background: #e3eaec;
            border: 1px solid #dee4e7;
        }
        #msgBox .list h3 span {
            position: absolute;
            left: 6px;
            top: 6px;
            background: #fff;
            line-height: 28px;
            display: inline-block;
            padding: 0 15px;
        }
        #msgBox .list ul {
            overflow: hidden;
            zoom: 1;
        }
        #msgBox .list ul li {
            float: left;
            clear: both;
            width: 100%;
            border-bottom: 1px dashed #d8d8d8;
            padding: 10px 0;
            background: #fff;
            overflow: hidden;
        }
        #msgBox .list ul li.hover {
            background: #f5f5f5;
        }
        #msgBox .list .userPic {
            float: left;
            width: 50px;
            height: 50px;
            display: inline;
            margin-left: 10px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }
        #msgBox .list .content {
            float: left;
            width: 400px;
            font-size: 14px;
            margin-left: 10px;
            font-family: arial;
            word-wrap: break-word;
        }
        #msgBox .list .userName {
            display: inline;
            padding-right: 5px;
        }
        #msgBox .list .userName a {
            color: #2b4a78;
        }
        #msgBox .list .msgInfo {
            display: inline;
            word-wrap: break-word;
        }
        #msgBox .list .times {
            color: #889db6;
            font: 12px/18px arial;
            margin-top: 5px;
            overflow: hidden;
            zoom: 1;
        }
        #msgBox .list .times span {
            float: left;
        }
        #msgBox .list .times a {
            float: right;
            color: #889db6;
            display: none;
        }
        .tr {
            overflow: hidden;
            zoom: 1;
        }
        .tr p {
            float: right;
            line-height: 30px;
        }
        .tr * {
            float: left;
        }
    </style>

  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的 jQuery 留言板的示例代码: HTML 代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>jQuery 留言板</title> <link rel="stylesheet" href="style.css"> </head> <body> <div id="container"> <h1>jQuery 留言板</h1> <form id="message-form"> <label for="name">姓名:</label> <input type="text" id="name" name="name" required> <label for="message">留言:</label> <textarea id="message" name="message" required></textarea> <input type="submit" value="提交"> </form> <ul id="message-list"> <!-- 这里将显示留言列表 --> </ul> </div> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script src="app.js"></script> </body> </html> ``` CSS 代码: ```css body { font-family: Arial, sans-serif; } #container { margin: 0 auto; max-width: 600px; padding: 20px; } h1 { text-align: center; } label { display: inline-block; margin-bottom: 5px; } input[type="text"], textarea { display: block; margin-bottom: 15px; width: 100%; } input[type="submit"] { display: block; margin: 15px auto; } ul { list-style: none; padding: 0; } li { margin-bottom: 20px; border: 1px solid #ccc; padding: 10px; } li h2 { margin-top: 0; } li p { margin-bottom: 0; } ``` JavaScript 代码: ```javascript $(function() { // 获取留言列表 getMessages(); // 提交留言 $('#message-form').submit(function(event) { event.preventDefault(); // 获取表单数据 var name = $('#name').val(); var message = $('#message').val(); // 发送 AJAX 请求 $.ajax({ type: 'POST', url: 'submit.php', data: { name: name, message: message }, success: function(response) { // 清空表单 $('#name').val(''); $('#message').val(''); // 刷新留言列表 getMessages(); } }); }); // 获取留言列表 function getMessages() { $.ajax({ type: 'GET', url: 'get.php', success: function(response) { // 清空留言列表 $('#message-list').empty(); // 显示留言列表 $.each(response, function(index, message) { var li = '<li><h2>' + message.name + '</h2><p>' + message.message + '</p></li>'; $('#message-list').append(li); }); } }); } }); ``` PHP 代码(submit.php): ```php <?php // 获取表单数据 $name = $_POST['name']; $message = $_POST['message']; // 将留言保存到数据库或文件中,这里只是简单地输出到控制台 echo '姓名:' . $name . '<br>'; echo '留言:' . $message . '<br>'; ?> ``` PHP 代码(get.php): ```php <?php // 从数据库或文件中获取留言列表,这里只是简单地返回一个数组 $messages = array( array( 'name' => '张三', 'message' => '你好,世界!' ), array( 'name' => '李四', 'message' => '今天天气不错。' ), array( 'name' => '王五', 'message' => '明天去旅游。' ) ); // 将留言列表转换成 JSON 格式并输出 echo json_encode($messages); ?> ``` 上述代码中,submit.php 和 get.php 分别用于提交留言和获取留言列表。在实际应用中,应该将留言保存到数据库或文件中,而不是简单地输出到控制台。 此外,还需要注意防止 XSS 攻击。可以使用 PHP 的 htmlspecialchars 函数将留言内容中的特殊字符转换成 HTML 实体,如下所示: ```php $message = htmlspecialchars($_POST['message'], ENT_QUOTES, 'UTF-8'); ``` 以上就是一个简单的 jQuery 留言板的示例代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值