javascript - focusout() 无法使用 jQuery 运行

标签 javascript jquery html

我正在尝试评估输入字段中内容的长度。如果内容的长度为0,我想应用一些CSS样式。

When I remove the if statement, this function works on focus out. Can you see where I've gone wrong with my syntax?

$( "input" ).focusout(function() {                   
     var n = $( "input" ).length;                   
     if (n == 0) {                   
         $( '.enteremail' ).css( "transform", "translateY(0px)");                   
     }                   
});

最佳答案

您必须检查输入的值:

$('input').focusout(function() {
     // Check the value of the affected ("this") input
     var n = $(this).val().length;
     if (n === 0) {                   
         $('.enteremail').css('transform', 'translateY(0px)');                   
     }                   
});

不是文档中输入字段的数量

关于javascript - focusout() 无法使用 jQuery 运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52701531/

相关文章:

javascript - 从 Javascript 函数中提取变量 (Phonegap)

html - CSS 转换而不转换其内容

php - 在 MySQL+PHP 中过滤数据库表的搜索以获得每次搜索最接近的结果

javascript - Electron(Atom Shell)应用程序中的用户设置存储在哪里?

javascript - 是否有更短的方法来测试变量是否已声明且不为空?

javascript - 使用 JQuery/JavaScript 下载最新版本的页面?

jquery - CSS JQuery PHP 菜单问题

javascript - 我的 jquery 中出现 NaN 错误

javascript - 如何动态创建包含类属性的列表

javascript - 将 x-editable 源放在 html 而不是 JavaScript 中