Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

全局监听click中的e.preventDefault与特定元素绑定click冲突 #86

Open
lizhongzhen11 opened this issue Mar 28, 2020 · 0 comments

Comments

@lizhongzhen11
Copy link
Owner

如题,最近在做一个响应式的网站,移动端菜单栏会移到左侧垂直展示,其实我做了两个菜单,移动端显示移动端的,pc显示pc的,互不干涉。

本来也没啥问题,但是移动端,左侧同时还有个筛选框,可收缩的,我在window上绑了个 click 事件,用来监听点击关闭筛选框的,本来也没啥,但是测试时突然发现,移动端菜单栏的点击事件死活不起作用!!!

起初我还以为是css问题,以为是下面的内容盖住了它,当我把所有盖住的全给 display: none 后,发现没用!!!

后来我突然想到,我给全局绑过 click 事件,原代码如下:

// 点击非 filter 区域关闭
  handleClickOut(e: any) {
    e.preventDefault()
    if (e.target.dataset.filter) {
      return
    }
    this.showMobileFilter = false
  }

我试着把 e.preventDefault() 去掉,果然能点了!!!
当时我就懵逼了!!!
我依稀记得 e.preventDefault() 这个只是阻止默认事件传递啊?难道我记错了?

遂翻了下 MDN —— event.preventDefault,用 checkbox 做了示例,突然想到我的是 <a> 标签,一开始想用 href 进行跳转的,结果死活不行,现在才意识到,原来是这样啊!!!

说到底学艺不精罢了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant