js模拟事件

参考:https://q.cnblogs.com/q/74438/
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
<!DOCTYPE html>
<html>
<head>
    <title>123</title>  
</head>
<body>
    <input type="text" id="inputID1" />

    <script>

	
	function syf(target, type)
	{
		target.focus();
        var doc = target.ownerDocument;  
        //创建键盘事件对象
        var customEventObject = undefined;
		
		customEventObject = doc.createEvent("UIEvents"); //or eventType = HtmlEvents           
		customEventObject.initEvent(type, true, true);
		customEventObject.view = null;
		customEventObject.altKey = false;
		customEventObject.ctrlKey = false;
		customEventObject.shiftKey = false;
		customEventObject.metaKey = false;		

        target.dispatchEvent(customEventObject)
	}
    //模拟
    function simulateKeyPress(target, type, keycode, charcode) {
        target.focus();
        var doc = target.ownerDocument;
        var Event = doc.defaultView.Event;

        //创建键盘事件对象
        var customEventObject = undefined;
        try {
            try {

                customEventObject = new Event(type);
            } catch (e) {
                customEventObject = doc.createEvent("HTMLEvents");
            }

            customEventObject.initEvent(type, true, true);
            customEventObject.shiftKey = false;
            customEventObject.altKey = false;
            customEventObject.metaKey = false;
            customEventObject.ctrlKey = false;
            customEventObject.keyCode = keycode;
            customEventObject.charCode = charcode;
            customEventObject.which = customEventObject.keyCode;
            // if( customEventObject.initKeyEvent ){
            //     customEventObject.initKeyEvent(type, true ,true,document.defaultView , false ,false ,false ,false ,0, parseInt('A',10) );

            // }else{
            //     console.log("------------ initKeyboardEvent---------------------");
            //     customEventObject.initKeyboardEvent(type,true ,true,document.defaultView , false ,false ,false ,false ,0, parseInt('A',10) );
            // }
        } catch (exception) {
            console.log(exception);
            try {
                customEventObject = doc.createEvent('Events');
            } catch (uiException) {
                customEventObject = doc.createEvent("UIEvents"); //or eventType = HtmlEvents
            } finally {
                customEventObject.initEvent(type, true, true);
                customEventObject.view = null;
                customEventObject.altKey = false;
                customEventObject.ctrlKey = false;
                customEventObject.shiftKey = false;
                customEventObject.metaKey = false;
                customEventObject.keyCode = keycode;
                customEventObject.charCode = charcode;
            }
        }

        target.dispatchEvent(customEventObject)
    };

    // test code
    // 随便写个html,放个input(type=text) ,然后监听该input的keydown或者keypress事件查看下是否被触发就可以了
    var txtobject = document.getElementById("inputID1");
   
    /*
    txtobject.addEventListener('input', function (e) {
        console.log("successful input");
        console.log(e.keyCode);
        //console.log(e.charCode);
        txtobject.value = e.charCode;
    }, false);
	*/
	
	txtobject.addEventListener('keydown', function (e) {
        console.log("successful keydown");
        console.log(e.keyCode);
        //console.log(e.charCode);
        //txtobject.value = e.charCode;
    }, false);
	
     function test() {
		//txtobject.value="what";
        syf(txtobject,'keydown','105', '0');
    }	
    </script>
	
	
    <button οnclick="test()" style="width:100px;height:30px" value="tt"></button>
	
	<script type="text/javascript">
	/*function syf1(target, type){target.focus();var doc = target.ownerDocument;var customEventObject = undefined;customEventObject = doc.createEvent("UIEvents");customEventObject.initEvent(type, true, true);customEventObject.view = null;customEventObject.altKey = false;customEventObject.ctrlKey = false;customEventObject.shiftKey = false;customEventObject.metaKey = false;target.dispatchEvent(customEventObject)}var txtobject = document.getElementById("inputID1");txtobject.value="what";syf1(txtobject,'input','105', '0');
	*/
	</script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值