在选择中隐藏第一个选项 [英] Hide first option in select

查看:93
本文介绍了在选择中隐藏第一个选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下拉菜单中有动态生成的选项.我想隐藏第一个选项,因为这不是必需的.是否有跨浏览器兼容的方法来实现这一目标.无论使用jquery还是css,任何解决方案都是好的.

I have options in a drop down which are generated dynamically. I want to hide first option as that is not needed. Is there any cross browser compatible method to achieve this. Any solution would be good whether using jquery or css.

Ahmar

推荐答案

$("#my-drop-down-select-element-id").find("option").eq(0).remove();

您可以使用上述代码删除<select />元素中的第一个<option />元素.

You can remove the first <option /> element in a <select /> element with the above code.

使用CSS隐藏<option />元素是有问题的.有关更多信息,请参见下面的链接.

Using CSS to hide <option /> elements is problematic. See the link below for more on this.

要分解以上功能链:

  1. 选择<select />元素. (无双关语)
  2. 在先前选择的元素中找到<option />个元素.我使用find()是因为如果有选项组,则children()将不起作用.
  3. 仅过滤到第一个<option />元素.
  4. 从DOM中删除该元素.
  1. Select the <select /> element. (No pun intended)
  2. Find <option /> elements within the previously selected element. I used find() because if there are option groups then children() won't work.
  3. Filter down to only the first <option /> element.
  4. Remove that element from the DOM.

这绝对是以前问过的,需要更多阅读:

This has definitely been asked before, for some more reading: How to hide a <option> in a <select> menu with CSS?

这篇关于在选择中隐藏第一个选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆