gpt4 book ai didi

javascript - 如何将js函数添加到仅当前面的元素处于焦点时才显示的元素

转载 作者:行者123 更新时间:2023-11-28 16:10:53 25 4
gpt4 key购买 nike

这是一个演示:http://jsfiddle.net/t9aAf/

任务是单击菜单中的一个元素,以便输入字段填充所选值。但它不起作用。

HTML:

<div class="wrapper">

<input type="text">

<div class="hints-menu">
<span class="hints-title"><span>Title 1</span></span>
<span class="hint" title="Some title">Item 1</span>
<span class="hint" title="Some title">Item 2</span>
<span class="hint" title="Some title">Item 3</span>
<span class="hints-title"><span>Title 2</span></span>
<span class="hint" title="Some title">Item 4</span>
<span class="hint" title="Some title">Item 5</span>
<span class="hints-title"><span>Title 3</span></span>
<span class="hint" title="Some title">Item 6</span>
<span class="hint" title="Some title">Item 7</span>
<span class="hint" title="Some title">Item 8</span>
<span class="hint" title="Some title">Item 9</span>
</div>

</div>

CSS:

.wrapper {
width: 260px;
margin: 30px auto;
}
input {
width: 252px;
height: 28px;
padding: 0 4px;
line-height: 20px;
border: 1px solid #ddd;

border-radius: 4px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
}
input:focus {
outline: none;
}
input:focus + .hints-menu {
display: block;
}
.hints-menu {
background: #fff;
position: relative;
display: none;
width: 240px;
margin-top: 10px;
padding: 10px;
border: 1px solid #ddd;

border-radius: 4px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
}
.hints-menu:before,
.hints-menu:after {
bottom: 100%;
left: 83%;
border: solid transparent;
content: "";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
}
.hints-menu:before {
border-color: transparent;
border-bottom-color: #ddd;
border-width: 9px;
margin-left: -9px;
}
.hints-menu:after {
border-color: transparent;
border-bottom-color: #fff;
border-width: 8px;
margin-left: -8px;
}
.hints-title,
.hint {
display: block;
width: 100%;
height: 22px;
line-height: 22px;
color: #555;
}
.hints-title {
position: relative;
margin: 0 0 15px;
text-align: center;
font-size: 15px;
border-bottom: 1px solid #555;
}
.hints-title span {
background: #fff;
position: absolute;
top: 50%;
right: 10%;
padding: 0 5px;
}
.hint {
font-size: 14px;
cursor: pointer;
}
.hint:hover {
color: #6fa024;
}

JS:

$(document).on("click",".hint",function(){
alert("haha");
alert($(this).val());
});

因为我还是编码新手。如果能帮我改正错误,我将不胜感激!谢谢你!

最佳答案

将以下声明添加到您的代码中:http://jsfiddle.net/UL9q9/ .它会防止菜单在鼠标悬停时被隐藏,即使输入已丢失:焦点。

input:not(:focus) + .hints-menu:hover {
display: block;
}

关于javascript - 如何将js函数添加到仅当前面的元素处于焦点时才显示的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24878356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com