首頁 >web前端 >js教程 >react如何阻止冒泡失敗

react如何阻止冒泡失敗

coldplay.xixi
coldplay.xixi原創
2020-11-17 14:08:092015瀏覽

react阻止冒泡失敗的方法:1、在沒有涉及到原生事件註冊只有react事件時,用【e.stopPropagation()】阻止冒泡;2、需要用到【e.nativeEvent. stopImmediatePropagation()】方法。

react如何阻止冒泡失敗

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 (
      <div
        className="test-box"
        onClick={this.handleClickTestBox}
      >
        <div
          onClick={this.handleClickTestBox2}
        >
          <div
            onClick={this.handleClickTestBox3}
          >
          </div>
        </div>
      </div>
    );
  }
}
export default App;

2、當用document.addEventListener註冊了原生的事件後,用e .stopPropagation()是無法阻止與document之間的冒泡,這時候需要用到e.nativeEvent.stopImmediatePropagation()方法,程式碼如下:

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

3、阻止合成事件與非合成事件(除了document)之間的冒泡,以上兩種方式都不適用,需要用到e.target判斷, 程式碼如下:

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

相關免費學習推薦:JavaScript(影片)

以上是react如何阻止冒泡失敗的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn