开发者

Is it possible add or change CSS properties using jQuery?

开发者 https://www.devze.com 2023-04-02 23:35 出处:网络
Specifically, a开发者_开发知识库 class or id within the css. Would you use something similar to $(window).height()?It\'s not clear exactly what you want, but as a general rule all the styles applied

Specifically, a开发者_开发知识库 class or id within the css.

Would you use something similar to $(window).height()?


It's not clear exactly what you want, but as a general rule all the styles applied to a given element can be accessed in JavaScript using something like:

element.style.<property-name>

So using native JavaScript you can do:

var elemStyles = document.getElementById("someId").style;
var styleWidth = elemStyles.width;

Assuming at least one element with a given CSS class and a framework that can select elements by class, you can similarly do:

var elemStyles = $(".someClass")[0].style;
var styleWidth = elemStyles.width;

Or depending upon what (if any) JavaScript framework you are using, there may be specialized methods that you can use to access/inspect various CSS attributes for a given element.

Note that any of these methods will bring back all the styles applied to the element, whether they are coming from the CSS file, from inline CSS declarations, or added programmatically by a script on the page. If you want to get just the styles inherited from the CSS file, then things get a bit trickier.


yes its possible

if you would like to receive other css properties check this out http://api.jquery.com/css

you would do somethig like this

var cssvalue = $(selector).css(propertyName);


This will probably help you, too. Esp. if you want to do it without jQuery: How do you read CSS rule values with JavaScript?

0

精彩评论

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