博客列表 >jq的DOM操作与事件操作02的有感记录

jq的DOM操作与事件操作02的有感记录

不露声色
不露声色原创
2021年04月15日 10:21:09590浏览

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

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

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