简体   繁体   English

将图像添加到选择选项

[英]Adding image to a select option

I am using document.createElement('option') in otion.text . 我在otion.text使用document.createElement('option') I need to append image with text. 我需要附加带文字的图像。

Below is how I am doing it, without much success: 以下是我的工作方式,但没有取得多大成功:

var mySelect = document.getElementById('mySelect'),
    newOption = document.createElement('option');

newOption.value = 'bmw';

if (typeof newOption.innerText === 'undefined') {
    newOption.textContent = 'BMW' < img src = "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash1/t5/27599546049768_1093999943_q.jpg" > ;
} else {
    newOption.innerText = 'BMW' < img src = "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-ash1/t5/27599546049768_1093999943_q.jpg" > ;
}

mySelect.appendChild(newOption);

and my HTML is: 我的HTML是:

<select id="mySelect">

</select>

How to add image and text to newOption.innerText ? 如何将图像和文本添加到newOption.innerText

You can't display images in a <option></option> you should take a look at a plugin for this; 您无法在<option></option>显示图像,您应该看一下这个插件; maybe take a look at this: http://tympanus.net/codrops/2012/10/04/custom-drop-down-list-styling/ 也许看看这个: http//tympanus.net/codrops/2012/10/04/custom-drop-down-list-styling/

As TryingToImprove said you cannot display images inside <option> but you also have another problem. 正如TryingToImprove所说,您无法在<option>显示图像,但您还有另一个问题。 You are not putting your strings together correctly. 你没有正确地把你的字符串放在一起。 Regardless, this has already been answered SO located here . 无论如何,这已经在这里得到了回答。 Hope this helps. 希望这可以帮助。

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

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