JS 模擬鼠標事件mouse over、click

 

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="renderer" content="webkit"/>
    <meta name="force-rendering" content="webkit"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <title>test mouse event</title>
	<style>
	body,html{
	background:#777;
	 
	}
	</style>
	
</head>
  <body>
 document.querySelector(".ttt").click()
 
 
 --------------------
iiiiiiiiii

mouseover 

mousedown
<br/>
<div class="ttt" style="background:red;" onclick="show()">
8888888888
</div>


<script>

document.body.addEventListener("mouseover", function () { console.log("mouseover");});
document.body.addEventListener("mousedown", function () { console.log("mousedown");});
document.body.addEventListener("mouseenter", function () { console.log("mouseenter");});
document.body.addEventListener("click", function () { console.log("click");});


// 隨意按下一個鍵盤按鍵
document.onkeydown = function(){
	console.log('keydown')
}
document.onkeypress= function(){
	console.log('onkeypress')
}
document.onkeyup = function(){
	console.log('onkeyup')
}
// keydown  onkeypress  onkeyup 




function show(){
   
   alert(000);

}


setInterval(function(){ 

//mouse over
  var event = document.createEvent("MouseEvents");
      event.initEvent('mouseover', true, false);
     document.body.dispatchEvent(event);
	 console.log('createEvent');
 //click  
const event2 = document.createEvent('Events');
event2.initEvent( 'click', true, false );
document.body.dispatchEvent(event2);


}, 3000);

</script>


 </body>

</html>

  

參考:https://www.cnblogs.com/h2285409/p/15989615.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章