首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当焦点不集中时隐藏div

当焦点不集中时隐藏div
EN

Stack Overflow用户
提问于 2016-10-08 01:54:59
回答 2查看 1.7K关注 0票数 1

在drupal中,编辑用户配置文件表单时,当我们单击密码字段(焦点)时,就会出现这个div。

代码语言:javascript
复制
<div class="password-suggestions description" style="display: block;">
Security hints and tips :
<ul><li>Tip #1 </li>
<li>Tip #2</li></ul></div>

它的内联样式从显示:无切换到阻塞。

但是当我们点击其他地方时,它不会切换回来,因此它会保持可见,并给页面添加一些不必要的杂乱。

因为我对javascript知之甚少,所以当不需要密码建议div时,我需要一些帮助来隐藏它。

下面是处理这个问题的js:

代码语言:javascript
复制
        Drupal.behaviors.password = {
      attach: function (context, settings) {
        var translate = settings.password;
        $('input.password-field', context).once('password', function () {
          var passwordInput = $(this);
          var innerWrapper = $(this).parent();
          var outerWrapper = $(this).parent().parent();

          // Add identifying class to password element parent.
          innerWrapper.addClass('password-parent');

          // Add the password confirmation layer.
          $('input.password-confirm', outerWrapper).parent().prepend('<div class="password-confirm">' + translate['confirmTitle'] + ' <span></span></div>').addClass('confirm-parent');
          var confirmInput = $('input.password-confirm', outerWrapper);
          var confirmResult = $('div.password-confirm', outerWrapper);
          var confirmChild = $('span', confirmResult);

          // Add the description box.
          var passwordMeter = '<div class="password-strength"><div class="password-strength-text" aria-live="assertive"></div><div class="password-strength-title">' + translate['strengthTitle'] + '</div><div class="password-indicator"><div class="indicator"></div></div></div>';
          $(confirmInput).parent().after('<div class="password-suggestions description"></div>');
          $(innerWrapper).prepend(passwordMeter);
          var passwordDescription = $('div.password-suggestions', outerWrapper).hide();

          // Check the password strength.
          var passwordCheck = function () {

            // Evaluate the password strength.
            var result = Drupal.evaluatePasswordStrength(passwordInput.val(), settings.password);

            // Update the suggestions for how to improve the password.
            if (passwordDescription.html() != result.message) {
              passwordDescription.html(result.message);
            }

            // Only show the description box if there is a weakness in the password.
            if (result.strength == 100) {
              passwordDescription.hide();
            }
            else {
              passwordDescription.show();
            }

            // Adjust the length of the strength indicator.
            $(innerWrapper).find('.indicator').css('width', result.strength + '%');

            // Update the strength indication text.
            $(innerWrapper).find('.password-strength-text').html(result.indicatorText);

            passwordCheckMatch();
          };

          // Check that password and confirmation inputs match.
          var passwordCheckMatch = function () {

            if (confirmInput.val()) {
              var success = passwordInput.val() === confirmInput.val();

              // Show the confirm result.
              confirmResult.css({ visibility: 'visible' });

              // Remove the previous styling if any exists.
              if (this.confirmClass) {
                confirmChild.removeClass(this.confirmClass);
              }

              // Fill in the success message and set the class accordingly.
              var confirmClass = success ? 'ok' : 'error';
              confirmChild.html(translate['confirm' + (success ? 'Success' : 'Failure')]).addClass(confirmClass);
              this.confirmClass = confirmClass;
            }
            else {
              confirmResult.css({ visibility: 'hidden' });
            }
          };

          // Monitor keyup and blur events.
          // Blur must be used because a mouse paste does not trigger keyup.
          passwordInput.keyup(passwordCheck).focus(passwordCheck).blur(passwordCheck);
          confirmInput.keyup(passwordCheckMatch).blur(passwordCheckMatch);
        });
      }
    };
EN

回答 2

Stack Overflow用户

发布于 2016-10-08 03:00:42

你可以通过CSS来隐藏它

代码语言:javascript
复制
.password-suggestions:not(:focus) {
    display: none !important;
}
票数 0
EN

Stack Overflow用户

发布于 2016-10-08 02:26:01

这里是用于隐藏安全提示div的html和jquery代码,请在输入字段中添加带有"myFunction()“函数的onfocusout事件。

下面是示例代码

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script></head>
<body>

Password: <input type="password" id="fname" onfocusout="myFunction()">
<div class="password-suggestions description" style="display: block;">
Security hints and tips :
<ul><li>Tip #1 </li>
<li>Tip #2</li></ul></div>


<script>
function myFunction() {
    $('div.password-suggestions').hide();
}
</script>

</body>
</html>

你的js代码

代码语言:javascript
复制
 function myFunction() {
        $('div.password-suggestions').hide();
    }

和html代码

代码语言:javascript
复制
<input type="text" id="fname" onfocusout="myFunction()">
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39927922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档