如果禁用 javascript,如何在任何 X/HTML 元素上提供 css 样式?
我想从外部 css 文件来做到这一点。我在主 css 中有一个 div#abc,但我想仅在禁用 javascript 时设置此 div 不显示
i want to do it from external css file. i have a div#abc in main css but i want to set display none for this div only if javascript is disabled
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
怎么样
?如果您确实想在外部 CSS 文件中执行此操作,请将其移至
no_js.css
中,并在link
元素(而不是 style 属性)中引用该文件。然而,大多数情况下,反过来更方便:设置禁用 JS 的默认值,然后让 JS 更改类名(例如)以更新样式。 jQuery 示例:
How about
? If you really want to do it in an external CSS file, move this into
no_js.css
and reference this file in alink
element instead of the style attribute.Mostly however, it is more convenient to go the other way round: Set the defaults for disabled JS and then let JS change class names (for example) to update the style. jQuery example:
我会让 div style="display: none;"默认情况下,然后用 javascript 显示它。
这样,如果您没有启用 javascript,则不会显示该 div。我已经使用内联 CSS 展示了它,但您也可以使用 CSS 类来实现它,并且只需从元素中删除该类即可。
这种方式将使其能够使用单个 jQuery 语句来处理页面上需要 JavaScript 的所有元素。
I would make the div style="display: none;" by default then show it with javascript.
This way, if you don't have javascript enabled, the div won't be shown. I've shown it using inline CSS but you could also do it with a CSS class and simply remove the class from the element.
This way would make it work with a single jQuery statement for all elements on the page that require javascript.
将您想要显示的元素放在 noscript 标签内,只有在禁用 javascript 时才会显示该元素。
Put the element you want to appear, inside of the noscript tag, and it will be displayed only if javascript is disabled.
您要做的就是在 javascript 中创建一个事件,将规则的样式从隐藏设置为不隐藏。让 CSS 始终将该元素设置为隐藏。如果他们关闭了 javascript,它永远不会取消隐藏它,因此它会被 CSS 隐藏。
What you would do is have an event in your javascript that would set the style of the rule from hidden to unhidden. Have the CSS always set that element as hidden. If they have javascript turned off, it never unhides it and thus it stays hidden by the CSS.