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

document.addEventListener('click', handler)没有效果,不会触发点击事件的回调处理 #14200

Closed
waitInHope opened this issue Jul 18, 2023 · 3 comments
Labels
F-react Framework - React T-tt Target - 编译到字节跳动小程序 V-3 Version - 3.x

Comments

@waitInHope
Copy link

相关平台

字节跳动小程序

小程序基础库: 2.89.0.0
使用框架: React

复现步骤

在页面组件中添加全局的点击事件
useEffect(() => {
document.addEventListener('click', () => {
console.log('全局点击事件');
})
}, []);

期望结果

点击屏幕后触发事件

实际结果

点击屏幕后没有响应

环境信息

👽 Taro v3.6.8


  Taro CLI 3.6.8 environment info:
    System:
      OS: macOS 12.2.1
      Shell: 5.8 - /bin/zsh
    Binaries:
      Node: 14.21.3 - ~/.nvm/versions/node/v14.21.3/bin/node
      Yarn: 1.22.19 - ~/.nvm/versions/node/v14.21.3/bin/yarn
      npm: 6.14.18 - ~/.nvm/versions/node/v14.21.3/bin/npm
    npmPackages:
      @tarojs/cli: 3.6.8 => 3.6.8 
      @tarojs/components: 3.6.8 => 3.6.8 
      @tarojs/helper: 3.6.8 => 3.6.8 
      @tarojs/plugin-framework-react: 3.6.8 => 3.6.8 
      @tarojs/plugin-html: ^3.6.8 => 3.6.8 
      @tarojs/plugin-platform-alipay: 3.6.8 => 3.6.8 
      @tarojs/plugin-platform-h5: 3.6.8 => 3.6.8 
      @tarojs/plugin-platform-jd: 3.6.8 => 3.6.8 
      @tarojs/plugin-platform-qq: 3.6.8 => 3.6.8 
      @tarojs/plugin-platform-swan: 3.6.8 => 3.6.8 
      @tarojs/plugin-platform-tt: 3.6.8 => 3.6.8 
      @tarojs/plugin-platform-weapp: 3.6.8 => 3.6.8 
      @tarojs/react: 3.6.8 => 3.6.8 
      @tarojs/runtime: 3.6.8 => 3.6.8 
      @tarojs/shared: 3.6.8 => 3.6.8 
      @tarojs/taro: 3.6.8 => 3.6.8 
      @tarojs/taro-loader: 3.6.8 => 3.6.8 
      @tarojs/webpack5-runner: 3.6.8 => 3.6.8 
      babel-preset-taro: 3.6.8 => 3.6.8 
      eslint-config-taro: 3.6.8 => 3.6.8 
      react: ^18.0.0 => 18.2.0 

补充信息

是否taro的document对象模拟的addEventListener方法有问题

@taro-bot2 taro-bot2 bot added F-react Framework - React T-tt Target - 编译到字节跳动小程序 V-3 Version - 3.x labels Jul 18, 2023
@xuanzebin
Copy link
Member

小程序没有 document 或者 window,因此本身就不支持这种方式的事件监听,因此 document.addEventListener('click', handler) 不会生效。

@waitInHope
Copy link
Author

小程序没有 document 或者 window,因此本身就不支持这种方式的事件监听,因此 document.addEventListener('click', handler) 不会生效。

但是Taro有模拟document对象,只是针对document.addEventListener()有问题
官网文章链接:https://docs.taro.zone/docs/taro-dom#document
本地代码调试也存在document对象nodeName: "#document"

@z3151zaq
Copy link

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-react Framework - React T-tt Target - 编译到字节跳动小程序 V-3 Version - 3.x
Projects
None yet
Development

No branches or pull requests

3 participants