如何在select标记内为选项元素添加鼠标悬停事件?
它适用于多选项选择的选择,但不支持单选项选择的选择。 有这种现象的原因吗?
下面是要复制的示例: sample to see in action
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div>
<select id="pet-select" multiple>
<option value="">--Please choose an option--</option>
<option onmouseover="console.log('hello1')" value="dog">Dog</option>
<option onmouseover="console.log('hello1')" value="cat">Cat</option>
</select>
</div>
<div style="margin-top:30px">
<select id="pet-select">
<option value="">--Please choose an option--</option>
<option onmouseover="console.log('hello2')" value="dog">Dog</option>
<option onmouseover="console.log('hello2')" value="cat">Cat</option>
</select>
</div>
</body>
</html>
答案 0 :(得分:0)
很抱歉,我不知道原因,但我可以帮助您找到解决方案。
我已将css
添加到id
的{{1}}中,然后将样式添加到了pet-select > option
事件中:
因此,代码如下:
onhover
#pet-select[multiple='multiple']>option:hover {
background-color: blue !important;
color: white !important;
}