12.事件派发(dispatchEvent)
下例仔细剖析了dispatchEvent。
例 12.1(DispatchEventFireIEFF.html)
<HTML>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</HEAD>
<BODY>
<select οnchange="alert('select onchange event');" id="select1">
<option>1</option>
<option>2</option>
</select>
<INPUT TYPE="button" value="按这" οnclick="clickButton()">
<script type="text/javascript">
function clickButton() {
alert("click button");
var t = document.getElementById('select1')
if (document.all) {
t.fireEvent("onchange");
alert("ie");
}
else {
var evt = document.createEvent('HTMLEvents');
evt.initEvent('change', true, true);
t.dispatchEvent(evt);
}
}
更多请见:http://www.mark-to-win.com/tutorial/js_4_dispatchEvent.html
javascript当中事件派发(dispatchEvent)的用法
最新推荐文章于 2024-04-23 17:04:17 发布