![](/img/trans.png)
[英]event.stopPropagation(); to hide dropdown, but not when it is clicked
[英]Redirect happens when clicked inside of the <a> tag, despite $event.stopPropagation
<a href="https://example.com/" target="_blank">
Documentation <i ng-click="$ctrl.hideDocumentation($event)" class="fa fa-times remove"></i>
</a>
控制器:
function hideDocumentation($event) {
$event.stopPropagation();
///// MAKE SOME API CALLS
}
單擊<i>
標記時如何防止重定向到https://example.com/
?
<a href="" target="_blank" onClick="handleClickFunction">
Documentation <i ng-click="$ctrl.hideDocumentation($event)" class="fa fa-times remove</i>
</a>
function hideDocumentation($event) {
$event.stopPropagation();
///// MAKE SOME API CALLS
}
即:將您的 clickHandler 移動到實際的<a/>
它仍在觸發並完全按照設計的目的進行導航...如果您在做完一些事情后仍想導航,則可以使用window.location = www.next-location.com
來自點擊處理程序內部的window.location = www.next-location.com
。
通過將您的圖標包裝在 aa 標簽中,您實際上是在將其設為鏈接……但您似乎需要區分行為。 在我看來,如果可能的話,這些應該分開。 當您確實想要導航時,盲目調用 preventDefault 可能會產生意想不到的行為。 我將創建一個onClick
處理程序來確定在這兩種情況下需要發生什么。 您可以只准確讀取點擊的內容(a 或 i)或執行類似這樣的操作來確定點擊的內容:
<a href="" target="_blank" onClick="($event) => $handleClick($event, 'a')">
Documentation <i ng-click="$handleClick($event, 'i')" class="fa fa-times remove</i>
</a>
function hideDocumentation($event, el) {
if (el === 'a') {
window.location = www.next-location.com
return
}
// else handle the non-nav scenario
$event.preventDefault();
$ctrl.hideDocumentation
///// MAKE SOME API CALLS
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.