博客列表 >为留言板添加字数实时统计与禁止超出功能; 2. 自选一些字符串和数组方法进行实例演示

为留言板添加字数实时统计与禁止超出功能; 2. 自选一些字符串和数组方法进行实例演示

小丑0o鱼
小丑0o鱼原创
2021年07月20日 17:26:39585浏览
  1. 留言板添加字数与限制字符

    留言板

  1. const comment = document.querySelector('.comment');
  2. const content = comment.content;
  3. const submitBtn = comment.submit;
  4. const commentList = document.querySelector('.list');
  5. 添加字数,限制字符,新留言前置
  6. submitBtn.onclick = (ev) => {
  7. let value = content.value.trim();
  8. if (value.length > 0 && value.length <= 100) {
  9. const newComment = document.createElement("li");
  10. newComment.textContent = value;
  11. newComment.style.borderBottom = "1px solid white";
  12. newComment.style.minHeight = "3em";
  13. 新留言前置,留言成功
  14. commentList.prepend(newComment);
  15. alert("left message successful");
  16. 清空留言,无内容或超字数
  17. content.value = null;
  18. content.focus();
  19. } else {
  20. alert("no content or too many words");
  21. content.focus();
  22. return false;
  23. }
  24. };
  25. 添加删除留言button
  26. newComment.append(deleteBtn);
  27. const deleteBtn = document.createElement(“button”);
  28. deleteBtn.textContent = delete”;
  29. deleteBtn.style.float = right”;
  30. deleteBtn.classList.add(“del-btn”);
  31. 确定是否删除
  32. deleteBtn.onclick = function () {
  33. if (confirm(“are you sure delete”)) {
  34. this.parentNode.remove();
  35. alert("delete successful");
  36. content.focus();
  37. return false;
  38. }
  39. };

2. 字符串和数组 concat()拼装 slice(start, end)取子串 substr(start, size): 取子串 trim(): 删除字符串二边的空白字符 将字符串打散成数组

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议