Created
September 18, 2016 08:22
-
-
Save chengjianhua/bd24fdaa7b46d71c80199e0b70038722 to your computer and use it in GitHub Desktop.
React check clicked target is whether contained in a another node.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
handleOutSideClick(event) { | |
const {onRowUnSelected} = this.props; | |
const isDescendantOfRoot = ReactDOM.findDOMNode(this.tbody).contains(event.target); | |
if (!isDescendantOfRoot) { | |
event.stopPropagation(); | |
this.setState({ | |
selectedRowIndex: -1 | |
}, () => { | |
onRowUnSelected && onRowUnSelected(); | |
}); | |
} | |
} | |
componentDidMount() { | |
if (document.addEventListener) { | |
document.addEventListener('click', this.handleOutSideClick, true); | |
} else { | |
document.attachEvent('onclick', this.handleOutSideClick); | |
} | |
} | |
componentWillUnmount() { | |
if (document.addEventListener) { | |
document.addEventListener('click', this.handleOutSideClick, true); | |
} else { | |
document.attachEvent('onclick', this.handleOutSideClick); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
判断点击的元素节点是否在指定的结点的内部.