当输入框获得焦点时保持下拉菜单可见

3

我的导航栏有类似于登录和搜索的下拉“字段集”:

<div class="nav-button" id="nav-box">
        <a class="inside-link">
            <span id="inside-text">Sign in</span>
        </a>
        <fieldset id="menu-box" class="menu-box">
            <form method="post" id="forms" class="forms" action="checklogin.php">
                <label for="username">Username or email</label>
                <input type="text" id="username" name="username" value="" title="username" tabindex="4">

                <label for="password">Password</label>
                <input type="password" id="password" name="password" value="" title="password" tabindex="5">

                <input type="submit" id="small-btn" value="Sign in" tabindex="6">
                <input type="checkbox" id="remember" name="remember_me" value="1" tabindex="7">
                <label for="remember">Remember me</label>
                <br />
                <a href="#"id="resend_password_link">Forgot your password?</a> 
                <a id='forgot_username_link' title="If you remember your password, try logging in with your email" href="#">Forgot your username?</a> 
            </form>
        </fieldset>
    </div>

我有一个这里的实例:http://jsfiddle.net/WBrns/5/

当像“搜索”、“用户名”和“密码”这样的输入框获得焦点时,我希望相关的下拉菜单不会消失,以便用户无需在输入时一直将鼠标放置于下拉菜单内。

在 CSS 中的第 288 行是我们的第一次尝试,显然不起作用。我的网站已经包括了 jQuery,因此任何基于 js/jquery 的解决方案都可以接受(因为我认为它不可能通过纯 CSS 实现)

谢谢!


你可以在输入框上添加"focusin"和"focusout"监听器,以便在获得焦点时移除CSS类,或在失去焦点时重新附加CSS类,从而允许隐藏菜单。 - MatRt
2个回答

0
改进Shaz的答案,您可以命名模糊事件以防止多个模糊事件附加到相同的输入。我还建议使用类名和CSS来显示和隐藏下拉菜单,以便您可以利用CSS过渡效果。
JS
$('input').focus(function () {
  var $this = $(this);
  var $drop = $this.parents('.drop');

  $drop.addClass('open');
  $this.bind('blur.closeDrop', function () {
    $drop.removeClass('open');
    $this.unbind('blur.closeDrop');
  });
});

CSS

.drop {
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}
.drop.open {
  opacity: 1;
  pointer-events: auto;
}

0
在您的hover样式中,确保属性具有!important命令,然后使用下面的代码,记得将id和class替换为您需要的内容:
$("input").focus(function () { that=this;
    $(this).parent(".drop").css("display", "block");

    $(this).blur(function() {
        $(that).parent(".drop").css("display", "none"); 
    });
})

您可以在此处查看示例:http://jsfiddle.net/WBrns/12/

如果用户开始输入,即使他们将鼠标移开,下拉菜单也不应该消失。但是,如果他们在下拉菜单外单击,它将被隐藏。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接