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. 将字符串打散成数组

更多相关文章

  1. 留言板,字符串和数组方法 ----0407
  2. 【DB笔试面试462】如何将一个数字转换为字符串并且按照指定格式
  3. Python正则表达式快速学习
  4. Python:字符串输出三种格式
  5. 【DB笔试面试467】Oracle中行列互换有哪些方法?
  6. 【DB笔试面试458】在Oracle中,如何判断一个字符串是否全为数字?
  7. __format__
  8. 字符串类型内置方法
  9. 【DB笔试面试494】Oracle中哪个包用于生成随机值?

随机推荐

  1. 详解如何用WPF图形解锁控件ScreenUnLock
  2. ASP.NET中怎样用MVC5的MiniProfiler对MVC
  3. C++ 之 Asio 库
  4. C#中关于RabbitMQ应用的图文代码详解
  5. ASP.NET MVC中SignalR用法讲解
  6. C++函数与指针
  7. 关于ASP.NET如何利用AjaxPro完成前端跟后
  8. 介绍MVC、MVP和MVVM的区别与用法
  9. 学习asp.net的学习顺序与学习内容分享
  10. asp.net如何利用ashx生成图形验证码的实