解決div中嵌套元素時,二者點擊事件同時觸發


以Angualr為例:

<div class="previewImg" (click)="closeImg()">
    <img id="leftArrow" *ngIf="mid > left" (click)="leftImg($event)" src="../../../assets/community/leftArrow@3x.png"/>
    <img id="pre" src="{{imageUrl}}"/>
    <img id="rightArrow" *ngIf="mid < right" (click)="rightImg($event)" src="../../../assets/community/rightArrow@3x.png"/>
</div>

點擊left或right時同時會觸發外層的closeImg()事件,此時需阻止JS的冒泡事件。

    closeImg() {
        this.imageUrl = '';
    }
    leftImg(e) {
        alert('left!');
        e.stopPropagation();
    }
    rightImg(e) {
        alert('right!')
        e.stopPropagation();
    }
e.stopPropagation();調用該語句即可只觸發內部的點擊事件。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM