javascript - 选择菜单 "fake"选项

标签 javascript jquery html select

我想知道,是否可以在默认情况下将“选择一个”显示为选定选项,但在用户单击以打开选择菜单时从选项列表中隐藏?我的意思是,类似于 placeholder用于文本输入字段。反正有没有用js或其他东西做的?提前致谢

更新

假设我们有选择菜单

<select>
<option value"">Select one...</option>
<option value"1">Option 1 </option>
...
</select>

我要做的是删除 <option value"">Select one...</option>当用户打开菜单时从选项列表开始,当用户关闭菜单时返回到默认状态。

最佳答案

可能是这样的:

<SELECT NAME="browser" VALUE="" 
  onchange="var opt=this.options[0];if(opt.getAttribute('role')==='placeholder'&&!opt.selected)opt.parentNode.removeChild(opt);">
    <option role="placeholder" value="">Which Web browser do you use most often?</option>
    <OPTGROUP LABEL="Firefox">
      <OPTION LABEL="2.0 or higher">
        Firefox 2.0 or higher
      </OPTION>
      <OPTION LABEL="1.5.x">Firefox 1.5.x</OPTION>
      <OPTION LABEL="1.0.x">Firefox 1.0.x</OPTION>
    </OPTGROUP>
    <OPTGROUP LABEL="Microsoft Internet Explorer">
      <OPTION LABEL="7.0 or higher">
        Microsoft Internet Explorer 7.0 or higher
      </OPTION>
      <OPTION LABEL="6.x">Microsoft Internet Explorer 6.x</OPTION>
      <OPTION LABEL="5.x">Microsoft Internet Explorer 5.x</OPTION>
      <OPTION LABEL="4.x">Microsoft Internet Explorer 4.x</OPTION>
    </OPTGROUP>
    <OPTGROUP LABEL="Opera">
      <OPTION LABEL="9.0 or higher">Opera 9.0 or higher</OPTION>
      <OPTION LABEL="8.x">Opera 8.x</OPTION>
      <OPTION LABEL="7.x">Opera 7.x</OPTION>
    </OPTGROUP>
    <OPTION>Safari</OPTION>
    <OPTION>Other</OPTION>
  </SELECT>
带有自定义标签 role="placeholder"

OPTION 必须首先放在 SELECT 中才能正常工作。

关于javascript - 选择菜单 "fake"选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7414429/

相关文章:

javascript - 从 jquery 插件调用函数

html - 我可以在 CSS 中创建一个底部有弧形的 div 吗?

javascript - 使用 Chrome 查找与 HTML 元素关联的所有 js 事件

javascript - Angular 文本 ng-minlength 当不为空时?

javascript - FloatingActionButton z-index 更改延迟

javascript - 如何覆盖 jQuery ui 可排序的拖动事件?

javascript - 如何在 Perl 中向 ajaxError 返回数据

javascript - 使用 Javascript/jQuery 在当前窗口后面打开一个窗口

Javascript 个性测验麻烦使用 'this' 和 data-* 属性

Javascript/jQuery "Dangle"动画