Angular2, what is the correct way to disable an anchor element?

前端 未结 10 1239
死守一世寂寞
死守一世寂寞 2020-11-27 17:51

I\'m working on an Angular2 application, and I need to display -- but disable an HTML element. What is the corr

10条回答
  •  离开以前
    2020-11-27 18:19

    Specifying pointer-events: none in CSS disables mouse input but doesn't disable keyboard input. For example, the user can still tab to the link and "click" it by pressing the Enter key or (in Windows) the ≣ Menu key. You could disable specific keystrokes by intercepting the keydown event, but this would likely confuse users relying on assistive technologies.

    Probably the best way to disable a link is to remove its href attribute, making it a non-link. You can do this dynamically with a conditional href attribute binding:

    
       {{ link.name }}
    
    

    Or, as in Günter Zöchbauer's answer, you can create two links, one normal and one disabled, and use *ngIf to show one or the other:

    
        {{ link.name }}
        {{ link.name }}
    
    

    Here's some CSS to make the link look disabled:

    a.disabled {
        color: gray;
        cursor: not-allowed;
        text-decoration: underline;
    }
    

提交回复
热议问题