美文网首页
事件传递和冒泡

事件传递和冒泡

作者: GaoXiaoGao | 来源:发表于2020-12-10 16:56 被阅读0次

    1.从最外层捕获,依次传递到最内层,从内层依次向最外层冒泡

    <view class="view1" capture-bind:tap="catchview1" bindtap="tapview1">
      <view class="view2" capture-bind:tap="catchview2" bindtap="tapview2">
        <view class="view3" capture-bind:tap="catchview3" bindtap="tapview3">
        </view>
      </view>
    </view>
    
    
      catchview1(){
        console.log('capture1');
      },
      tapview1(){
        console.log('tapview1');
      },
    
      catchview2(){
        console.log('capture2');
      },
      tapview2(){
        console.log('tapview2');
      },
    
      catchview3(){
        console.log('capture3');
      },
      tapview3(){
        console.log('tapview3');
      },
    
    
    点击后,依次打印
    
    capture1
    capture2
    capture3
    tapview3
    tapview2
    tapview1
    
    

    如果让view1捕获到事件, 不向里传递,则需要使用
    capture-catch:tap="catchview1"

    <view class="view1" capture-catch:tap="catchview1" bindtap="tapview1">
      <view class="view2" capture-bind:tap="catchview2" bindtap="tapview2">
        <view class="view3" capture-bind:tap="catchview3" bindtap="tapview3">
        </view>
      </view>
    </view>
    
    这样再点击时,只打印了
    capture1
    

    修改成以下这样

    <view class="view1" capture-bind:tap="catchview1" bindtap="tapview1">
      <view class="view2" capture-bind:tap="catchview2" catchtap="tapview2">
        <view class="view3" capture-bind:tap="catchview3" bindtap="tapview3">
        </view>
      </view>
    </view>
    则打印结果如下
    capture1
    capture2
    capture3
    tapview3
    tapview2
    

    相关文章

      网友评论

          本文标题:事件传递和冒泡

          本文链接:https://www.haomeiwen.com/subject/lrihgktx.html