我想讓用戶動態設置樣式,但我也想要獲取某些樣式的值。例如,我想檢查用戶是否將H1元素的顏色設置爲「橙色」。使用AngularJS動態設置樣式時訪問樣式屬性
我的HTML
<body ng-controller="mainController">
<h1 id="myH1">Hello World!</h1>
<textarea ng-model="outputCss"></textarea>
<style media="screen" type="text/css">
{{outputCss}}
</style>
</body>
我的JavaScript,在mainController
var myH1Style = document.getElementById('myH1').style;
$scope.$watch('outputCss', function(newValue, oldValue){
if (myH1Style.color == "orange"){
alert("Nice work!");
}
console.log(myH1Style.color);
});
實施例的用戶輸入
#myH1 {
color: orange;
}
h1 {
font-weight: 900;
}
問題是,if條件永遠無法滿足,並且控制檯日誌始終爲空。我如何閱讀使用此方法添加的CSS屬性?