禁用 Javascript 的 CSS 切换
当 Javascript 被禁用时,我在切换 css 时遇到问题。
我有这样的代码:
<style type="text/css">.nonjsonly{display:none;}.jsonly{display:inline;}</style>
<noscript><style type="text/css">.nonjsonly{display:inline}.jsonly{display:none}</style></noscript>
Witch 现在正在工作,但是 W3C 不批准它,因为 noscript 在 html head 中是非法的(并且代码在 head 中)。
知道如何制作吗?
提前致谢
I´m having a problem switching css when Javascript is disabled.
I have this code:
<style type="text/css">.nonjsonly{display:none;}.jsonly{display:inline;}</style>
<noscript><style type="text/css">.nonjsonly{display:inline}.jsonly{display:none}</style></noscript>
Witch is working right now, but W3C doesn´t approve it, as noscript is illegal in html head (And the code is in head).
Any idea how it can be made?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
处理此问题的更合适方法是首先放置非 js CSS,不带任何
标记,然后是为支持 JavaScript 的客户端加载样式表的脚本。然后,支持 JS 的 css 应级联以覆盖基本表。
A more appropriate way to handle this is to place your non-js CSS first, without any
<noscript>
tag, followed by a script which loads a stylesheet for JavaScript-capable clients. The JS-capable css should then cascade to override the basic sheet.将默认的 css 放在第一位:
这样非 JS 样式适用于所有人。然后使用一些 JS 动态加载“js-enabled”样式:
这将覆盖非 js 样式。
Put the default css first:
so the non-JS styles apply to everyone. Then use some JS to dynamically load the "js-enabled" styles:
which will override the non-js styles.
.js
添加到html
元素.js
前缀例如:
变得
更好,使用 Modernizr 来检测更高级的功能(它也会自动添加
.js
)。.js
to thehtml
element via JavaScript.js
Ex:
becomes
Even better, use Modernizr to detect more advanced features (it'll automatically add
.js
too).您可以执行类似的操作
如果 Javascript 可用, ,将“jsActive”类写入到 Body 元素中。
您没有“noJavascipt”类,但您可以使用 CSS 选择并构建您的 Funciton
you can do something like this
that script write the "jsActive" Class to the Body Element if Javascript is avalible.
You don´t have a "noJavascipt" Class, but you can select with CSS and build you Funcitons