Blogger Information
Blog 52
fans 1
comment 1
visits 38614
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
为留言板添加字数实时统计与禁止超出功能; 2. 自选一些字符串和数组方法进行实例演示
小丑0o鱼
Original
589 people have browsed it
  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(): 删除字符串二边的空白字符 将字符串打散成数组

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!