繁体   English   中英

将图像添加到选择选项

[英]Adding image to a select option

我在otion.text使用document.createElement('option') 我需要附加带文字的图像。

以下是我的工作方式,但没有取得多大成功:

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);

我的HTML是:

<select id="mySelect">

</select>

如何将图像和文本添加到newOption.innerText

您无法在<option></option>显示图像,您应该看一下这个插件; 也许看看这个: http//tympanus.net/codrops/2012/10/04/custom-drop-down-list-styling/

正如TryingToImprove所说,您无法在<option>显示图像,但您还有另一个问题。 你没有正确地把你的字符串放在一起。 无论如何,这已经在这里得到了回答。 希望这可以帮助。

暂无
暂无

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

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