您将传递一个 CSS 选择器openSelect()
,它会为您打开select
元素。
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');
});
这是一个小提琴:http: //jsfiddle.net/Z48wF/1/
资料来源:如何使用 jquery @stackoverflow.com打开选择输入