博主信息
博文 22
粉丝 0
评论 0
访问量 19591
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
为留言板添加字数实时统计与禁止超出功能; 2. 自选一些字符串和数组方法进行实例演示
杰西卡妈妈
原创
745人浏览过

1. 留言板添加字数与限制字符

  • 写留言板

    <style>
    {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    }
    li {
    list-style: none;
    }
    body {
    background-color:rgb(174, 236, 236);
    color: #555;

    }
    .comment {
    width: 85%;
    margin: 1em auto;

    display: grid;
    gap: 0.5em;
    }
    .comment #content {
    resize: none;
    border:none;
    padding: 0.5em;
    outline: none;
    }
    .comment #content:focus,
    .comment #content:hover {

    box-shadow: 0 0 8px steelblue;
    transition: 0.6s;
    }
    .comment .submit {
    width: 30%;
    margin-left: auto;
    background-color: lightseagreen;
    border: none;
    outline: none;
    color: white;
    height: 2.5em;
    }
    .comment .submit:hover {
    background-color: seagreen;
    transition: 0.6s;
    cursor: pointer;
    }
    .list {

    width: 80%;
    /
    background-color: yellow; /
    margin: auto;
    padding: 1em;
    }

    .del-btn {
    background-color: wheat;
    color: red;
    padding:0.5em 1em;
    /
    height: 2.2em; */
    border:none;
    outline: none;
    }
    .del-btn:hover {
    cursor: pointer;
    background-color: lime;
    }

    </style>

  • 获取元素

    <script>
    1. const comment = document.querySelector('.comment');
    2. const content = comment.content;
    3. const submitBtn = comment.submit;
    4. const commentList = document.querySelector('.list');
  • 添加字数,限制字符,新留言前置

    submitBtn.onclick = (ev) => {

    1. let value = content.value.trim();
    2. if (value.length > 0 && value.length <= 100) {
    3. const newComment = document.createElement("li");
    4. newComment.textContent = value;
    5. newComment.style.borderBottom = "1px solid white";
    6. newComment.style.minHeight = "3em";
  • 新留言前置,留言成功

    commentList.prepend(newComment);

    1. alert("left message successful");
  • 清空留言,无内容或超字数

    content.value = null;
    1. content.focus();
    2. } else {
    3. alert("no content or too many words");
    4. content.focus();
    5. return false;
    6. }
    7. };
  • 添加删除留言button

    newComment.append(deleteBtn);
    const deleteBtn = document.createElement(“button”);
    deleteBtn.textContent = “delete”;
    deleteBtn.style.float = “right”;
    deleteBtn.classList.add(“del-btn”);
  • 确定是否删除

    deleteBtn.onclick = function () {
    if (confirm(“are you sure delete”)) {
    1. this.parentNode.remove();
    2. alert("delete successful");
    3. content.focus();
    4. return false;
    5. }
    6. };

2. 字符串和数组

  1. concat()拼装
  2. slice(start, end)取子串
  3. substr(start, size): 取子串
  4. trim(): 删除字符串二边的空白字符
  5. 将字符串打散成数组
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学