简体   繁体   English

HTML:我可以标记/突出显示下拉列表菜单项吗?

[英]HTML: Can I mark/highlight a drop-down list menu item?

Say I've got a list. 说我有一个清单。 I want to let the user know that one of the indices was the one that was previously set. 我想让用户知道其中一个索引是之前设置的索引。 I would like it so that when the user opens the drop down menu he can see that one of the options is marked so it looks different from the rest. 我希望这样当用户打开下拉菜单时,他可以看到其中一个选项被标记,因此它看起来与其他选项不同。

Is there a facility for this? 有这个设施吗? I won't bother with it if I have to hack up something ugly or re-implement the entire menu functionality to get this to work. 如果我不得不破解丑陋的东西或重新实现整个菜单功能以使其工作,我将不会烦恼。

If this is a element that has tags inside, you could try adding a specific class for option you want to highlight. 如果这是一个内置标签的元素,您可以尝试为要突出显示的选项添加特定类。

<option class="myoption"...

Then apply background color to this option through css. 然后通过css将背景颜色应用于此选项。

.myoption {background-color: red;}

I think this will not work in IE 6 and 7, because you cannot apply styles for selects in these versions of browsers. 我认为这在IE 6和7中不起作用,因为您无法在这些版本的浏览器中应用选择样式。

If you have < ul > and < li > elements for list, apply the same method decribed above. 如果列表中包含<ul>和<li>元素,请应用上述相同的方法。

Generally, this would be unnecessary as you would simply have the previously selected option pre-selected the next time the page loads when in state . 通常,这是不必要的,因为您只需在下次页面加载状态预先选择先前选择的选项。

However, if you want to show select-list options in a specific colour, then you can essentially give each option individual css styles, shown in an example here . 但是,如果要以特定颜色显示选择列表选项,则基本上可以为每个选项提供单独的css样式,如此处的示例所示。

Are you talking about a simple <select> -Dropdown? 你在谈论一个简单的<select> -Dropdown吗?

If so, it's pretty straightforward with CSS: 如果是这样,使用CSS非常简单:

<select>
    <option style="background:yellow">yellow</option>
    <option style="background:red">red</option>
    <option style="background:blue">blue</option>
</select>

Of course CSS-Classes will work, too. 当然CSS-Classes也可以。

Using the selected attribute on the option tag would be another solution. 在选项标签上使用selected属性将是另一种解决方案。 See 看到

http://www.w3schools.com/tags/att_option_selected.asp http://www.w3schools.com/tags/att_option_selected.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM