【问题标题】:Input field with dropdown Hints [closed]带有下拉提示的输入字段[关闭]
【发布时间】:2014-09-11 11:13:52
【问题描述】:

您好,我想创建一个带有提示菜单的 html 输入字段。效果应该如下图所示。单击输入字段后,将出现下拉菜单。

我想知道是否有任何插件或代码示例做类似的事情? 谢谢你!

【问题讨论】:

    标签: javascript jquery html web-deployment frontend


    【解决方案1】:

    这是一个FIDDLE

    <input type="text">
    
    <div class="hints-menu">
      <span class="hints-title">Title 1</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">Title 2</span>
      <span class="hint" title="Some title">Item 4</span>
      <span class="hint" title="Some title">Item 5</span>
      <span class="hints-title">Title 3</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>
    

    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 {
      margin: 5px 0;
      text-align: center;
      font-size: 15px;
    }
    .hint {
      cursor: pointer;
      font-size: 14px;
    }
    .hint:hover {
      color: #6fa024;
    }
    

    对于.hint 标题,您可以使用jQuery UI Tooltip

    【讨论】:

    • 哦,这正是我所需要的。非常感谢!
    • 不客气,我很高兴能帮上忙 ;)
    • 我在fiddle 中添加了jQuery UI Tooltip 示例。
    • 我正在阅读您的代码。以下代码中的“+”号是什么意思: input:focus + .hints-menu { display: block; }
    • + 用于在输入焦点时选择.hints-menu。更多关于CSS Selectors.
    猜你喜欢
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-16
    • 1970-01-01
    • 2011-04-13
    相关资源
    最近更新 更多