纯js实现点击一个事件后,触发另外一个事件的方法

来源:互联网 发布:合肥飞恒网络 编辑:程序博客网 时间:2024/06/05 18:03
我们在日常的开发中,经常会用到,当我点击一个按钮的click事件后,我想触发另一个按钮的click事件。
这时我们该怎么做呢。用过ext的人都知道,frieEvent方法。但是在使用手机开发的时候,ext对我们的作用就不是很大了。
虽然ext5.0出来了,对手机又支持的功能需求。但是你会为了一个很小的功能,去加载整个庞大的ext.js吗。就算你会使用,你的客户会买账吗。
并不一定会吧,因为你的网站加载速度慢,浪费流量,早就被对手取代了吧。
好吧,我们就废话少说,看看纯js是怎么实现的。看代码:
[html] view plaincopy
  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta charset="utf-8" />  
  5.         <title>helloworld</title>  
  6.     </head>  
  7.     <body>  
  8.         <div > hello world</div>  
  9.         <input id="world" type="button" value="hello world" onclick="world();"/>  
  10.         <input id="hello" type="button" value="hello HBuilder" onclick="alert('hello HBuilder');"/>  
  11.         <script type="text/javascript">  
  12.             function world(){  
  13.                 alert('hello world');  
  14.                 var evt = document.createEvent("MouseEvents");   
  15.                     evt.initEvent("click", true, true);   
  16.                     document.getElementById("hello").dispatchEvent(evt);   
  17.             }  
  18.         </script>  
  19.     </body>  
  20. </html>  

好吧,看看运行效果。


欢迎大家关注我的个人博客!!!!
如有不懂,疑问或者欠妥的地方,请加QQ群:135430763   进行反馈,共同学习!

0 0
原创粉丝点击