<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../js/jquery.min.js"></script>
</head>
<body>
<button>按钮</button>
<a href='http://www.baidu.com'>模拟点击</a>
<!-- jq中的模拟点击事件 -->
点击只会触发a链接的css样式,不会触发a链接的跳转事件
<script>
$('a').on('click',function(){
$(this).css('background','blue')
})
$('button').on('click',function(){
$('a').click()
})
</script>
<!-- 原生的模拟点击 -->
点击会触发css样式,同时会触发a链接的跳转事件
<script>
let btn=document.querySelector('button')
let as=document.querySelector('a')
as.onclick=function(){
this.style.backgroundColor='red'
}
btn.addEventListener('click',function(){
as.click()
})
</script>
</body>
</html>
原生和jquery模拟点击的区别
最新推荐文章于 2021-11-27 17:20:59 发布
本文探讨了使用jQuery和原生JavaScript实现模拟点击事件的方法。详细对比了两种方式下a链接的CSS样式变化及跳转行为的不同,揭示了模拟点击事件在前端开发中的应用技巧。
1980

被折叠的 条评论
为什么被折叠?



