简体   繁体   English

在选择时模拟点击事件(不适用于IE和FF)

[英]Simulate click event on select (not working for IE and FF)

I want to simulate click event on select using button. 我想模拟选择按钮上的点击事件。 I have inspired through the following link. 我通过以下链接获得了启发。 Simulate click on select element with jQuery 使用jQuery模拟选择元素的单击

On above thread this fiddle is working fine for chrome. 在上面的线程上,此小提琴对chrome正常工作。 I am facing an issue with same behavior for firefox and Internet explorer. 我正面临着与Firefox和Internet Explorer行为相同的问题。

I have edited the fiddle. 我已经编辑了小提琴。 This is fiddle but it seems it is not working. 这是摆弄,但似乎不起作用。 In this fiddle I am able to get into the function but actual click event is not firing on select. 在这个小提琴中,我能够进入该函数,但实际的click事件并未在select上触发。 I am missing pieces of code and because of that my code is not working. 我缺少一些代码,因此我的代码无法正常工作。 Any help with fill in the blanks. 任何帮助填写空白。

here is HTML code. 这是HTML代码。

<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>

This is Javascript. 这是Javascript。

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

// This isn't magic.
window.runThis = function () { 
var dropdown = document.getElementById('dropdown');
//showDropdown(dropdown);
eventFire(dropdown, 'click');
};
function eventFire(el, etype) {
alert('hi');
if (el.fireEvent) {
    el.fireEvent('on' + etype);
    el[etype]();
} else {
    var evObj = document.createEvent('Events');
    evObj.initEvent(etype, true, false, window);
    el.dispatchEvent(evObj);
}
}

Also I found couple of references for the same kind of operation. 我也发现了几种相同操作的参考。

HTML file HTML文件

    <button>Trigger the link below</button><br />
    <a href="http://www.yahoo.com/"  target="_blank">
    http://www.google.com/</a> (new window)
    <br>
        <select id="dropdown">
    <option value="Red">Red</option>
   <option value="Green">Green</option>
   <option value="Blue">Blue</option>
   </select>

This is javascript file. 这是javascript文件。

function eventFire(el, etype) {
alert('hi');
if (el.fireEvent) {
    el.fireEvent('on' + etype);
    el[etype]();
} else {
    var evObj = document.createEvent('Events');
    evObj.initEvent(etype, true, false);
    el.dispatchEvent(evObj);
}
}

document.getElementsByTagName("button")[0].onclick = function() {
var element = document.getElementsByTagName("select")[0];
eventFire(element, "click");
};

This above fiddle is also not working. 上面的小提琴也不起作用。 Please let me know if any more information needed. 请让我知道是否需要更多信息。

Above fiddle I am referencing from stackoverflow itself. 在小提琴之上,我指的是stackoverflow本身。 Because of my account limitation I can not paste direct link. 由于我的帐户限制,我无法粘贴直接链接。

Here is what I did: 这是我所做的:

$("#fire").click(function () {
    var e = document.createEvent("MouseEvents");
    e.initMouseEvent("mousedown");
    $("#dropdown")[0].dispatchEvent(e);
});

Here is the JSFiddle demo 这是JSFiddle演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM