在jQuery中创建一个CSS类 [英] Creating a CSS class in jQuery

查看:130
本文介绍了在jQuery中创建一个CSS类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信jQuery中的.addClass()函数将CSS类附加到当前选择,但我想知道我可以在jQuery中创建或定义一个CSS类,然后附加它?

I believe the .addClass() function in jQuery attaches a CSS class to the current selection, but I was wondering I could create or define a CSS class in jQuery, and then attach it?

推荐答案

实际上,您可以创建一个CSS规则,将影响当前页面上的所有元素。在大多数浏览器中,它应该像下面这样简单:

Actually, you can create a CSS rule that will affect all elements on the current page. In most browsers it should be as simple as:

var style = $('<style>body { background: green; }</style>')
$('html > head').append(style);

这可能会或可能不会在IE中工作,但您可以使用IE的专有 addRule

This may or may not work in IE, however you can use IE's proprietary addRule instead:

document.styleSheets[0].addRule('body', 'background: green', -1);

当然,这不会帮助您创建可以在网页之间共享的CSS文件,方便的方式影响大量元素的风格,而不需要迭代它们。

Naturally this will not assist you in creating css files that can be shared between webpages, but it is a handy way of affecting the style of a large number of elements without the need to iterate over them.

这篇关于在jQuery中创建一个CSS类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆