html - 焦点选择器,保持焦点在弹出窗口

标签 html css

早上好,我尝试创建的弹出窗口的样式有问题,单击按钮时会显示弹出窗口并在单击弹出窗口主体时保持焦点,尽管单击在其中弹出,弹出窗口失去焦点。

单击弹出窗口中的任何子元素时如何保持焦点在弹出窗口上?

非常感谢。

     <html>
       <head>

<style type="text/css">
#modal{display:none;}
#modal:focus {display:block;}
#modal:focus * #modal{display:block;}/*i thought maybe this would apply when any child element of #modal has          focus, although if i give a child element of modal a tabindex, it still doesn't work.*/
.num:focus + #modal{display:block;}
 </style>
</head>

<body>
  <div id="onetwo">
 <input type="button" id="btn1" class="num" tabindex="1" value="Click here"/>
<div id="modal" style="background-color:green;width:200px;height:200px;
position:absolute;top:40%;left:40%;" tabindex="2">
<input type="button" id="btn1" onclick="alert("Alerted")" value="Click.."/>
<input type="text" id="txt1" placeholder="some text"/>
</div>
</div>
</body>

</html>

最佳答案

问题是,一旦您点击弹出窗口中的子元素,您的按钮就不再处于焦点状态,弹出窗口将不会显示。

这不能仅在 CSS 中轻松完成。最好的方法是使用 javascript 并将点击处理程序添加到您的按钮以使用 CSS 显示和隐藏弹出窗口:

var btn = document.getElementById('btn1');

btn.onclick = function() {
    var popup = document.getElementById('modal');

    if(popup.style.display == 'block') {
        popup.style.display = 'none';
    }
    else {
        popup.style.display = 'block';
    }
}

See this Fiddle

关于html - 焦点选择器,保持焦点在弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24079419/

相关文章:

css - 背景色 Css

html - 为什么 <h3> 表现得像 <a>

javascript - react native 导航器 View 不呈现

html - 如何将 Bootstrap 底部导航栏移动到所需位置?

html - Sass CSS - 更改导航栏链接颜色

javascript - 使用传单 maxbounds 设置边界但不起作用

javascript - 在 HTML 中使用 JavaScript 复选框值

javascript - 禁用 Bootstrap 字体

jquery - Twitter bootstrap3 : HTML5, 非跨域iframe缩放宽高

javascript - 在我的 sidenav 上启用滚动