How to hide parts of HTML when JavaScript is disabled?

后端 未结 8 2095
伪装坚强ぢ
伪装坚强ぢ 2021-02-07 06:10

I\'m trying to accommodate users without JavaScript. (By the way, is it worth the effort nowadays?)

In one HTML file I\'d like part of it execute if scripts are on

8条回答
  •  情书的邮戳
    2021-02-07 06:33

    By the way, is it worth the effort nowadays?

    Yes, it's worth worrying about accessibility. You should use progressive enhancement where possible, i.e. make a basic version that works without scripting and then add the scripting functionality on top of it.

    A simple example would be a pop-up link. You could make one like this:

    link
    

    However, this link wouldn't work if someone, say, middle-clicked or tried to bookmark it, or had scripts disabled etc. Instead, you could do the same thing like this:

    link
    

    It's still not perfect, because you should separate the behavior and structure layers and avoid inline event handlers, but it's better, because it's more accessible and doesn't require scripting.

提交回复
热议问题