javascript - 如何在执行一次后停止javascript中的focus()函数?

标签 javascript jquery

我有一个 html 密码类型的输入字段;我创建了一个函数,在输入字段中显示 focus() 上的密码。但问题是我希望该函数只执行一次,然后用户可以更改输入,而不需要每次都使 focus() 工作。

这是脚本: 有一个测试:它显示提示在其中写入密码,然后脚本检查写入的密码是否等于 session 中实际用户的密码,如果是,则用户可以看到密码他点击了它,否则他会收到一条警告,告诉他不允许查看密码。

$('input[class=password]').focus(function(){
               this.type = 'password';
               $pwd=prompt('saisir votre mot de passe');
               if($pwd==='{{ search.password }}') {
                      this.type = 'text';
                     // this.blur();
                   this.off("focus");
                   }
                   else
                   {
                       alert("Vous ne pouvez pas voir les mots de passe!");
                       this.blur();
                   }

               }).blur(function(){
               this.type = "password";
           });

最佳答案

this 是 dom 元素,而不是 jQuery 对象。因此,要使用 off() 您需要更改:

this.off("focus");

致:

$(this).off("focus");

还有one()它只会执行一次事件处理程序:

$('input[class=password]').one('focus', function(){...

关于javascript - 如何在执行一次后停止javascript中的focus()函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43544252/

相关文章:

javascript - 隐藏一个元素,但保持其宽度,但用 javascript 将其高度设置为 0

javascript - Javascript如何比较 map 的键

javascript - 使用 contenteditable 在生成的表中触发 onkeyup

javascript - 获取当前 URL,并将其删除

javascript - 将对象映射到数组

javascript - 无法使用ajax将Google map 从一个页面加载到另一个页面的div中

javascript - 以 JSON 格式创建 res.cookie,并将值作为对象

jQuery 自定义点击

javascript - 循环遍历数组并使用每个数组值更改 html 元素

javascript - jQuery Force 为 iframe 设置 src 属性