【发布时间】:2014-10-16 16:08:58
【问题描述】:
我正在尝试在选择框选项具有焦点时发生若要发生的事情(写入987654324 @)。从 jsfiddle 代码中,您可以看到我已经让它在事后工作 - 也就是说,一旦选择了选项并将鼠标移回现在关闭的选择框。打开盒子后如何让它工作?
(以防万一,我有另一个函数绑定到同一个元素(选择框),该元素会触发“更改”事件。我想知道这是否是问题所在?)。
HTML:
<div id="Hint"></div>
<div id='select-container'>
<select name="parent_selection" id="parent_selection">
<option selected="selected">-- Select 1st option --</option>
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
<select name="child_selection" id="child_selection">
<option value="">-- Select 2nd option --</option>
</select>
</div>
JavaScript:
//Display hint for hovered selection in 1st dropdown
var elSelect_container, elHint; // Declare variables
elAppSelected = document.getElementById('parent_selection');
elHint = document.getElementById('Hint');
function Hint() {
var elAppSelected = this.options[this.selectedIndex].value;
if (elAppSelected === 'option1') {
elHint.innerHTML = 'Good choice';
}
if (elAppSelected === 'option2') {
elHint.innerHTML = 'Better choice';
}
if (elAppSelected === 'option3') {
elHint.innerHTML = 'Best Choice!';
}
}
//Create event listener: mouseover calls Hint()
elAppSelected.addEventListener('mouseover', Hint, false);
【问题讨论】:
-
选项元素并不始终支持鼠标悬停事件。 stackoverflow.com/questions/15038811/…
-
啊哈!线索。我的代码在 FF 中有效,但在 Chrome 或 IE 中无效。将调查解决方法。谢谢 Zentoaku。
标签: javascript