详见示例
代码
1
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
>
2 < html >
3 < head >
4 < title > JavaScript模拟用户单击事件 </ title >
5 <!-- 引入jQuery -->
6 < script src ="http://sj.yaolanimage.cn/js/jquery-1.3.2.min.js" type ="text/javascript" ></ script >
7
8 < script type ="text/javascript" >
9 /* *
10 * 模拟用户单击事件 处理超链接的问题
11 */
12 $( function ()
13 {
14 $( " #btn " ).click( function ()
15 {
16 $( " #submit " ).click();
17
18 // 绑定单击事件
19 $( " #aLink " ).click( function ()
20 {
21 var a = window.open($( this ).attr( " href " ),$( this ).attr( " target " ));
22 a.focus();
23 });
24
25 // 触发单击事件(会执行所有绑定的单击事件处理函数)
26 $( " #aLink " ).click();
27 });
28 });
29 function mousePosition(ev){
30 if (ev.pageX || ev.pageY){
31 return {x:ev.pageX, y:ev.pageY};
32 }
33 return {
34 x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
35 y:ev.clientY + document.body.scrollTop - document.body.clientTop
36 };
37 }
38
39 function mouseMove(ev){
40 ev = ev || window.event;
41 var mousePos = mousePosition(ev);
42 document.getElementById( ' div1 ' ).value = mousePos.x;
43 document.getElementById( ' div2 ' ).value = mousePos.y;
44 }
45
46 document.onmousedown = mouseMove;
47 </ script >
48 < style >
49 div { border : 1px solid red ; height : 100px ; width : 100px ; }
50 </ style >
51 </ head >
52
53 < body >
54 < h3 > 请单击”Click Me"。测试提交按钮与超链接是否也被单击了。 </ h3 >
55 < button id ="btn" > Click Me </ button >
56 < form action ="#" >
57 < input id ="submit" type ="submit" value ="别点击此按钮提交" onclick ="alert('触发了提交按钮的单击事件!');" />
58 </ form >
59 < a id ="aLink" href ="http://www.google.cn" target ="_blank" onclick = "alert('触发了超链接的单击事件!');" > 代码触发超链接 </ a >
60 x轴坐标是: < input id ="div1" /> y轴坐标是: < input id ="div2" />
61 </ body >
62
63 </ html >
64
2 < html >
3 < head >
4 < title > JavaScript模拟用户单击事件 </ title >
5 <!-- 引入jQuery -->
6 < script src ="http://sj.yaolanimage.cn/js/jquery-1.3.2.min.js" type ="text/javascript" ></ script >
7
8 < script type ="text/javascript" >
9 /* *
10 * 模拟用户单击事件 处理超链接的问题
11 */
12 $( function ()
13 {
14 $( " #btn " ).click( function ()
15 {
16 $( " #submit " ).click();
17
18 // 绑定单击事件
19 $( " #aLink " ).click( function ()
20 {
21 var a = window.open($( this ).attr( " href " ),$( this ).attr( " target " ));
22 a.focus();
23 });
24
25 // 触发单击事件(会执行所有绑定的单击事件处理函数)
26 $( " #aLink " ).click();
27 });
28 });
29 function mousePosition(ev){
30 if (ev.pageX || ev.pageY){
31 return {x:ev.pageX, y:ev.pageY};
32 }
33 return {
34 x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
35 y:ev.clientY + document.body.scrollTop - document.body.clientTop
36 };
37 }
38
39 function mouseMove(ev){
40 ev = ev || window.event;
41 var mousePos = mousePosition(ev);
42 document.getElementById( ' div1 ' ).value = mousePos.x;
43 document.getElementById( ' div2 ' ).value = mousePos.y;
44 }
45
46 document.onmousedown = mouseMove;
47 </ script >
48 < style >
49 div { border : 1px solid red ; height : 100px ; width : 100px ; }
50 </ style >
51 </ head >
52
53 < body >
54 < h3 > 请单击”Click Me"。测试提交按钮与超链接是否也被单击了。 </ h3 >
55 < button id ="btn" > Click Me </ button >
56 < form action ="#" >
57 < input id ="submit" type ="submit" value ="别点击此按钮提交" onclick ="alert('触发了提交按钮的单击事件!');" />
58 </ form >
59 < a id ="aLink" href ="http://www.google.cn" target ="_blank" onclick = "alert('触发了超链接的单击事件!');" > 代码触发超链接 </ a >
60 x轴坐标是: < input id ="div1" /> y轴坐标是: < input id ="div2" />
61 </ body >
62
63 </ html >
64