开发者

Can I use CSS attribute values in javascript in rails?

Say I have a .box class in CSS:

.box
{
   height: 50px;
}

Now, I use javascript to allow the user to expand the box and make it small again. Currently I have the value of the box height (ie 50 px) hardcoded in javascript.

I'm wondering if there's a way where my javascript code can access the height attribute of the box class as defined in CSS so that if I change the height in CSS to 100px for example, I wouldn't have to go and manually change the corresponding variable in javascript

btw I'm开发者_C百科 using rails3 and jquery


With jQuery it's pretty easy, although you have to use some trickery to get around the fact that jQuery's height() function spits back 50px instead of 50.

Just for getting the object's height, try this:

$(".box").height();

It doesn't succumb to the issue I stated above. The following example does, and fixes it. It can also be used for every other CSS attribute.

var boxHeight = $(".box").css("height").replace("px", "");

Docs here.


document.getElementById("mybox").style.height = myheight + "px";
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜