关于jquery的click()方法
昨天,有个同事研究了以下jqury的click()方法,代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#ago').click(function () { $('#a01').click();
$('#a02').click(); });
});
$(function () {
$("#a01").click( function () {
window.open("http://www.baidu.com" ) });
})
$(function () {
$("#a02").click( function () {
window.open("http://www.g.cn" ) });
})
</script>,
</head>
<li><a id="a01" target="_blank" href="http://www.baidu.com">www.baidu.com</a></li>
<li><a id="a02" target="_blank" href="http://www.g.cn">www.g.cn</a></li>
<br>
<button id="ago" style="border: 1px solid black;cursor: pointer">更新</button> </body>
</html>
如上代码,在chrom和Firefox IE 完全不一样。在chrome上只能打开一个窗口,而其他浏览器在点击更新的时候,可以同时打开2个页面。
经查资料:
Safari/Chrom只有在标签input/button情况下,支持click方法,而我们的例子中非如上标签。所以,我们在调用click()方法的时候,出现问题。
既然不支持jquery的方法,那我们使用dom的dispatchEvent方法,这样就支持了。代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
function dispatch(el, type){
try{
var evt = document.createEvent('Event');
evt.initEvent(type,true,true);
el.dispatchEvent(evt);
}catch(e){alert(e)}
}
$(function () {
$("#ago").click(function () {
var a01 = document.getElementById('a01');
var a02 = document.getElementById('a02');
dispatch(a02, 'click');
dispatch(a01, 'click'); }) });
$(function () {
$("#a01").click( function () {
window.open("http://www.baidu.com" ) });
});
$(function () {
$("#a02").click( function () {
window.open("http://www.g.cn" ) });
})
</script>
</head>
<li><a id="a01" target="_blank" href="http://www.baidu.com">www.baidu.com</a></li>
<li><a id="a02" target="_blank" href="http://www.g.cn">www.g.cn</a></li>
<br>
<button id="ago" style="border: 1px solid black;cursor: pointer"><a id=""></a>更新</button> </body>
</html>
关于jquery的click()方法的更多相关文章
- jQuery 事件 click() 方法,dblclick() 方法
click() 方法 当点击元素时,会发生 click 事件. 当鼠标指针停留在元素上方,然后按下并松开鼠标左键时,就会发生一次 click. click() 方法触发 click 事件,或规定当发生 ...
- JQuery实现click事件绑定与触发方法分析
原生JS通过什么方法绑定click事件? 原生js有一下三种方法为DOM对象绑定click事件, 第一种,在html中添加 onclick属性,在此属性中添加要绑定的事件函数,如下, 这种方法为htm ...
- jquery click()方法模拟点击事件对a标签不生效的解决办法
阅读数:8971 <a href="www.baidu.com"></a> 1 问题分析 点击A标签本身,并不会触发跳转到指定链接的事件,就是说,我们平时都 ...
- jquery click()方法模拟点击事件对a标签不生效
if(e.keyCode == 13) { $items.eq(index).click(); return; } 搜索框下拉列表模拟点击时间,使用上述代码不能触发链接跳转 1,页面使用了bootst ...
- jquery click()方法 语法
jquery click()方法 语法 作用:当点击元素时,会发生 click 事件.当鼠标指针停留在元素上方,然后按下并松开鼠标左键时,就会发生一次 click.click() 方法触发 click ...
- 避免jquery的click多次绑定方法
$("#xxx").click(function(){}) 这样只是会在原click方法中继续添加新方法: 当然解决办法是解绑: $("#xxx").unbin ...
- jquery 通过submit()方法 提交表单示例
jquery 通过submit()方法 提交表单示例: 本示例:以用户注册作为例子.使用jquery中的submit()方法实现表单提交. 注:本示例仅提供了对表单的验证,本例只用选用了三个字段作为测 ...
- jquery.on()超级方法
$.on()方法是jquery1.7之后的一个超级方法,将事件绑定和事件委托整合到一个函数中去,支持绑定多个事件,并且可以绑定自定义事件.使用起来很方便. demo传送门 事件委托 首先说一下事件委托 ...
- jquery的ready方法(DOM是否加载完)详解与使用
jquery的ready方法(准备DOM触发)还是比较复杂的,我们先看流程图:
随机推荐
- CodeForces765A
A. Neverending competitions time limit per test:2 seconds memory limit per test:512 megabytes input: ...
- linux基础学习之软件安装以及常用命令(三)
添加和查看用户: [root@localhost ~]# useradd anderson [root@localhost ~]# cat /etc/passwd 显示如下: [root@localh ...
- c#无边窗体实现移动的两种方式
转载:http://blog.csdn.net/dxsh126/article/details/2940226 首先,要用到一个WimdowsAPI函数,因此必须引入 using System.Run ...
- PHP自定义函数&数组
<?php//生成随机数 和 时间函数//echo rand();//echo "<br>";//echo rand(0,10);//echo time();// ...
- 数组也继承了Object类
C++ 最根上的类有好多,也可以随便地定义.
- leetCode题解 Student Attendance Record I
1.题目描述 You are given a string representing an attendance record for a student. The record only conta ...
- jquery中的ajax方法
$.ajax()方法详解 jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为Strin ...
- 汽车学习---汽车知识大全【all】
汽车驱动/发动机/大灯 侧方位停车和倒车入库技巧 手动挡车型 换档8大技巧 常见的掉头情况注意事项 最全的左转,右转,待转,掉头方法 开车技巧大汇总,尤其适合新手司机 汽车常识扫盲,不做“汽车小白”! ...
- Java问题汇总(一) ----------2019/2/22
第一次写,就弄弄简单样式
- iis里URL重写重定向,http做301重定向https
上一篇文章写了iis和apache共用80端口,IIS代理转发apache. 因为第一次配置,这中间还是碰到了很多问题.这里记录下来,希望以后能避免错误. 使用Application Request ...