原因:
如果想知道某一個element的現時style,就需要用到getComputedStyle(elemnet,xxx) method.第二個參數xxx一定為null或不寫,如想查的是一般的element。
JS Fiddle:https://jsfiddle.net/js_Test4fun/qejv4yd0/17/
實習:
那麼,getComputedStyle(elemnet,xxx) method會返回什麼?Code:

Result:

得出一個object,包括這個element的所有style參數。
用法:

用window.getComputedStyle(p1)得出 object
再用其.getPropertyValue(“color”) method 找出property value
結果:
剛開始:
按下button後:

Good!得到想要和正確的 color 參數。
Reference:
-MDN 說明[1] https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle
沒有留言:
發佈留言