网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

react怎样阻止冒泡失败

这篇文章主要介绍react怎样阻止冒泡失败,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

成都创新互联公司为客户提供专业的成都网站设计、成都网站建设、程序、域名、空间一条龙服务,提供基于WEB的系统开发. 服务项目涵盖了网页设计、网站程序开发、WEB系统开发、微信二次开发、手机网站开发等网站方面业务。

react阻止冒泡失败的方法:1、在没有涉及到原生事件注册只有react事件时,用【e.stopPropagation()】阻止冒泡;2、需要用到【e.nativeEvent.stopImmediatePropagation()】方法。

react阻止冒泡失败的方法:

1、在没有涉及到原生事件注册只有react事件时,用e.stopPropagation()阻止冒泡,代码如下:

import React, { Component } from 'react';
import './App.css';
class App extends Component {
  handleClickTestBox = (e) => {
    console.warn('handleClickTestBox: ', e);
  }
  handleClickTestBox2 = (e) => {
    console.warn('handleClickTestBox2: ', e);
  }
  handleClickTestBox3 = (e) => {
    e.stopPropagation();
    console.warn('handleClickTestBox3: ', e);
  }
  render() {
    return (
      
); } } export default App;

2、当用document.addEventListener注册了原生的事件后,用e.stopPropagation()是不能阻止与document之间的冒泡,这时候需要用到e.nativeEvent.stopImmediatePropagation()方法,代码如下:

import React, { Component } from 'react';
import './App.css';
class App extends Component {
  componentDidMount() {
    document.addEventListener('click', this.handleDocumentClick, false);
  }
  handleDocumentClick = (e) => {
    console.log('handleDocumentClick: ', e);
  }
  handleClickTestBox = (e) => {
    console.warn('handleClickTestBox: ', e);
  }
  handleClickTestBox2 = (e) => {
    console.warn('handleClickTestBox2: ', e);
  }
  handleClickTestBox3 = (e) => {
    // 阻止合成事件的冒泡
    e.stopPropagation();
    // 阻止与原生事件的冒泡
    e.nativeEvent.stopImmediatePropagation();
    console.warn('handleClickTestBox3: ', e);
  }
  render() {
    return (
      
); } } export default App;

3、阻止合成事件与非合成事件(除了document)之间的冒泡,以上两种方式都不适用,需要用到e.target判断, 代码如下:

import React, { Component } from 'react';
import './App.css';
class App extends Component {
  componentDidMount() {
    document.addEventListener('click', this.handleDocumentClick, false);
    document.body.addEventListener('click', this.handleBodyClick, false);
  }
  handleDocumentClick = (e) => {
    console.log('handleDocumentClick: ', e);
  }
  handleBodyClick = (e) => {
    if (e.target && e.target === document.querySelector('#inner')) {
      return;
    }
    console.log('handleBodyClick: ', e);
  }
  handleClickTestBox = (e) => {
    console.warn('handleClickTestBox: ', e);
  }
  handleClickTestBox2 = (e) => {
    console.warn('handleClickTestBox2: ', e);
  }
  handleClickTestBox3 = (e) => {
    // 阻止合成事件的冒泡
    e.stopPropagation();
    // 阻止与原生事件的冒泡
    e.nativeEvent.stopImmediatePropagation();
    console.warn('handleClickTestBox3: ', e);
  }
  render() {
    return (
      
); } } export default App;

以上是react怎样阻止冒泡失败的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!


网页标题:react怎样阻止冒泡失败
标题URL:http://bjjierui.cn/article/gopsdc.html

其他资讯