Blogger Information
Blog 8
fans 1
comment 0
visits 4843
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
jq的DOM操作与事件操作02的有感记录
不露声色
Original
599 people have browsed it

看小猪老师写的行云流水甚是羡慕

但是咱得老老实实弄清楚到底是咋回事

  • this关键字指代当前的标签input
  • 其次我打印了$(this).next()[0]的信息,发现 这个tagName 是标签的大写
  • 最后注意的点就是 insertAfter 是兄弟元素的新增,而append是追加子元素
  1. const user = $("input[name='username']");
  2. user.blur = function(){
  3. // console.log("sss");
  4. let tip = "";
  5. //用户列表
  6. const users = ["admin", "peter", "zhu"];
  7. // console.log($(this).val().length);
  8. if ($(this).val().length === 0) {
  9. tip = "用户名不能为空";
  10. $(this).focus();
  11. }
  12. console.log($(this).next());
  13. //显示提示信息到页面中
  14. if ($(this).next()[0].tagName !== "SPAN") {
  15. $("<span></span>").html(tip).css({
  16. color: "red",
  17. fontSize: "14px",
  18. }).insertAfter($(this));
  19. }
  20. }
  21. //on('事件类型',回调函数)
  22. //事件监听 addEventListener()
  23. //input 检测用户的输入内容
  24. //当时输入后的blur会产生span标签,这里搞一个判断
  25. user.on("input", function () {
  26. if ($(this).next()[0].tagName === "SPAN") {
  27. $(this).next().remove();
  28. }
  29. });
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