开发者

Use css within jQuery?

How would you use CSS within a 开发者_如何学运维jQuery script, so that if javascript were disbaled within the browser both the jQuery script and CSS would not show up?


Take a look at this post: http://forum.jquery.com/topic/loading-stylesheets-on-the-fly


Another approach I've seen is to dynamically add a special css class to the body tag via javascript, and then preprend javascript-only CSS selectors with that css class.

eg:

<script>
document.body.className += ' scriptson';
</script>
<style>
.scriptson a {
    /* only applied if javascript is enabled */
}
</style>


Put the <script> and <link> tag in a <noscript> tag.

To do the opposite, use $("head").append("<link href="CSSFILE" />"); or of the sort so that if javascript is disabled, the script will not run and the css will not be included. Note though, because it takes Javascript time to load, it will be jumpy. It is not what you want, which is why the <noscript> is the better solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜