const user = $("input[name='username']");user.blur = function(){// console.log("sss");let tip = "";//用户列表const users = ["admin", "peter", "zhu"];// console.log($(this).val().length);if ($(this).val().length === 0) {tip = "用户名不能为空";$(this).focus();}console.log($(this).next());//显示提示信息到页面中if ($(this).next()[0].tagName !== "SPAN") {$("<span></span>").html(tip).css({color: "red",fontSize: "14px",}).insertAfter($(this));}}//on('事件类型',回调函数)//事件监听 addEventListener()//input 检测用户的输入内容//当时输入后的blur会产生span标签,这里搞一个判断user.on("input", function () {if ($(this).next()[0].tagName === "SPAN") {$(this).next().remove();}});
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号