点击打开选择选项

时间:2016-03-05 15:55:40

标签: javascript html select onclick

有没有办法打开选择选项onclick,而不是<select><option>元素?

<label onclick = 'openSelectOptions()'> Click Me </label>

1 个答案:

答案 0 :(得分:0)

你可以尝试这个,但不适用于所有浏览器。 它适用于Google Chrome浏览器

&#13;
&#13;
function openSelectOptions(){
  var element = $("#open")[0];
  var worked = false;
  if (document.createEvent) {
      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) {
      worked = element.fireEvent("onmousedown");
  }
  if (!worked) {
      alert("It didn't worked in your browser.");
  }
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="open">
    <option>nothing</option>
    <option>something</option>
    <option>anything</option>
</select>
<button onclick = 'openSelectOptions()'> Click Me </button>
&#13;
&#13;
&#13;