2
如何獲取樣式表/樣式應用的div樣式標籤並以編程方式忽略元素樣式?獲取繼承自類的元素樣式* not *樣式屬性
例如
var alice = 'alice ' + $('.alice').css('color');
var boblice = 'alice ' + $('.bob').css('color');
var bob = 'bob ' + $('.bob').css('color');
$('#result').text(
alice + '\n' + bob
);
$('#expected').text(
boblice + '\n' + bob
);
var elem1 = document.getElementById("alice");
alicestyle = window.getComputedStyle(elem1, null);
console.log(alicestyle);
.alice {
color:green;
}
.bob {
color:green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='alice' class="alice" style="color:red">
test
</div>
<div id = 'bob' class="bob">
test
</div>
result:
<div id="result">
</div>
expected:
<div id="expected">
</div>
getComputedStyle:
<div id="computed">
</div>
你的意思是像[**'getComputedStyle' **](https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle)? –
此鏈接可能會幫助https://stackoverflow.com/questions/16778814/read-css-property-from-stylesheet – Aravinder
@ibrahimmahrir不,getComputedStyle包含style屬性。 –