opacity and style undefined when accesing element in js but defined in css

前端 未结 4 1026
南方客
南方客 2021-01-19 14:27

With this fiddle http://jsfiddle.net/mungbeans/f2ne6/2/

why is the opacity undefined when accessed in js when its defined in the css?

I presume the answer is

4条回答
  •  余生分开走
    2021-01-19 15:28

    Chase is correct, but there's another problem in your code. The style property only contains styles that were set with the style attribute of the element, so Chase's solution will only go halfway to fixing your problem. What you want to do is use the getComputedStyle() function to get the runtime style of your element:

     function test(id) {
         var listElement = document.getElementById(id);
         var titles = listElement.getElementsByTagName("div");
         var style = getComputedStyle(titles[0]);
         alert( "Opacity: " + style.opacity );
     }
    

    See my updated jsfiddle here: http://jsfiddle.net/7vQ4A/1/

提交回复
热议问题