如何使用javascript在firefox中触发鼠标按下事件?

时间:2012-10-30 11:43:30

标签: javascript prototype

我想使用javascript触发组合框/选择框的鼠标按下事件。这是代码,它在crome中工作得很好,但在Firefox中却没有。对此有何帮助?

<select id="dropdown">
    <option value="Red">Red</option>
    <option value="Green">Green</option>
    <option value="Blue">Blue</option>
</select>
<br>
<button id="fire" type="button" onclick="runThis()">Show dropdown items</button>

// <select> element displays its options on mousedown, not click.
showDropdown = function (element) {
    var event;
    event = document.createEvent('MouseEvents');
    //event.initMouseEvent('mousedown', true, true, window);
    event.initMouseEvent('mousedown', true, true, window,0, 0, 0, 0, 0, false, false, false, false, 0, null);
    element.dispatchEvent(event);
};

// This isn't magic.
window.runThis = function () {
    var dropdown = document.getElementById('dropdown');
    showDropdown(dropdown);
};

提前致谢....