javascript - Some problems with react bubbling
淡淡烟草味
淡淡烟草味 2017-05-19 10:18:56
0
2
388
   fn1(){
         alert('父级');
     }
     fn2(ev){
         ev.stopPropagation();
         ev.nativeEvent.stopImmediatePropagation();

         alert("子集");
     }
     componentDidMount(){
         document.onclick=function(){
             alert('document');
         };

         this.refs['fa'].onclick=function(ev){
             //console.log(this);
             console.log(ev.target);
             alert("爷爷");
         }
     }

<p ref="fa" className="fa">
     <p onClick={this.fn1.bind(this)}>
     <p onClick={this.fn2.bind(this)}>aaaaaaaaaaa</p>
</p>

Only use e.stopPropagation() to prevent the parent event from bubbling. The fn1 event will not trigger, but the document event will trigger?
e.nativeEvent.stopPropagation() prevents bubbling to docuemnt but the parent's fn1 is triggered. Every time fa is clicked, it will be triggered and it will be triggered first. If e.target cannot be obtained, only capturing fn2 will have the result p aaa, So the question is how to trigger only outer events without capturing them? What is the approximate event mechanism of react events? Thank you for the solution·

淡淡烟草味
淡淡烟草味

reply all(2)
洪涛

No, these two are actually native methods.

stopImmediatePropagation and link stopPropagation. As for nativeEvent, it should be unique to ReactNative, and React should not have this thing.

巴扎黑

https://zhuanlan.zhihu.com/p/...You will understand after reading this link

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!