You will pass a CSS selector to openSelect()
and it will open the select
element for you.
var openSelect = function(selector){
var element = $(selector)[0], worked = false;
if (document.createEvent) { // all browsers
var e = document.createEvent("MouseEvents");
e.initMouseEvent("mousedown", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
worked = element.dispatchEvent(e);
} else if (element.fireEvent) { // ie
worked = element.fireEvent("onmousedown");
}
if (!worked) { // unknown browser / error
alert("It didn't worked in your browser.");
}
}
$(function(){ // when DOM is ready
// open .select element
openSelect('.select');
});
Here's a fiddle: http://jsfiddle.net/Z48wF/1/
Source: How to open the select input using jquery @stackoverflow.com
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…