简体   繁体   中英

js style properties returns blank of the style css

I want to use the div's css properties for my js conditional statement.

I have this code of css :

div.desc{
   position:relative;
   top:0px;
   left:0px;
}

now, I want to use it in my js so I try to alert the top.

alert(document.getElementsByClassName('desc')[0].style.top)

but I got the blank return.

Does my code done wrong? or it is possible that I can get the css property of the div class desc using js?

您可以使用jquery做到这一点。

alert($('.desc').eq(0).css('top'));

Does the first item that has the class desc have the style top set? If not it is inherited or inferred.

you can get it like this

var el = document.getElementsByClassName('desc')[0];
var comp = el.currentStyle || getComputedStyle(el, null);
alert( comp.top );

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM