2012-10-30 44 views
0

我想用javascript触发组合框/选择框的鼠标按下事件。这里是代码,它在crome中完美工作,但不在Firefox中。对此有何帮助?如何在firefox中使用javascript来触发鼠标事件?

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

在此先感谢....

+0

是'element.click()'不够好? – 2012-10-30 11:48:54

+0

@ H2CO3 - 不,我想要的是,当用户点击下拉某个选项,然后它加载数据在这里,并选择该选项,但我想保持下拉下来打开.. 比较这在克罗姆和FF和看到区别: http://jsfiddle.net/fz2sY/106/ – CPP

+0

我有同样的问题,找不到任何东西!从什么时候开始,在Firefox中没有'mousedown'工作? – WebWanderer

回答

相关问题