Html 添加带有选项标签的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5562149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:48:51  来源:igfitidea点击:

Adding images with option tag

htmlcss

提问by enthusiastic

I have a drop down list. I want to add images along with it. I tried adding imgtag inside tha optiontag... But still the images are not getting displayed. How do I do it using optiontag?

我有一个下拉列表。我想同时添加图像。我尝试img在 tha 标签内添加标签option......但图像仍然没有显示。我如何使用option标签做到这一点?

回答by BoltClock

It's not possible, as <option>only supports text.

这是不可能的,因为<option>只支持 text

You may have to roll your own drop-down control using complex HTML/CSS/JavaScript. How to do it may or may not be within the scope of your question.

您可能必须使用复杂的 HTML/CSS/JavaScript 滚动您自己的下拉控件。如何做到这一点可能在您的问题范围内,也可能不在。

Alternatively, you may use a non-repeating background-imageand apply some padding on the text to achieve a similar effect. But if each <option>is to have a unique image, your code is going to be polluted with a styleattribute for every single <option>. If you don't mind that, it's fine. Otherwise, roll your own somehow.

或者,您可以使用非重复background-image并在文本上应用一些填充来实现类似的效果。但是,如果每个<option>图像都具有唯一的图像,那么您的代码将被style每个<option>. 如果你不介意,没关系。否则,以某种方式推出自己的产品。