Is 'disabled' a valid attribute for an anchor tag

前端 未结 6 495
傲寒
傲寒 2020-12-03 16:38

If I have the following simple code segment:

click me &
6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 17:08

    Removing the href or setting it to '#' when you actually want to disable it is kind of a pain if the anchor is to be enabled later, because you need to reset the href to whatever value it should link to. Instead, I just add a disable attribute to the tag and a click event handler and some css. This way the anchor can easily be seen to be disabled, but if enabled where it would go.

    Yes, disabled isn't supported attribute by the anchor tab, but the CSS attribute selector does find it and so does jQuery's. So, while the following solution is a mixed jQuery/javaScipt/CSS, it does provide a somewhat nicer way to disable/enable anchors, which supports dynamically adding/removing the disabled attribute to/from the tag with javaScript. Note that this has only been tested and found to work in Chrome.

    
    
    
    

    Here is a codePen demo: https://codepen.io/howardb1/pen/XWrEKzP

提交回复
热议问题