對於針對此問題的可擴展解決方案,您還可以考慮在BEM實施中添加和刪除修改類。
//HTML
<div class='my-nice-class my-nice-class--dimensions_A'></div>
,那麼Css:
CSS:
.my-nice-class--dimensions_A {
max-width: 90%;
max-height: 90%;
}
.my-nice-class--dimensions_B {
max-width: 40%;
max-height: 40%;
}
然後JavaScript可以添加和刪除該班
//Javascript
var htmlEl = document.getElementsByClassName('my-nice-class')[0]; // in case you need the first element from the elements array
htmlEl.classList.add("my-nice-class--dimensions_B");
htmlEl.classList.remove("my-nice-class--dimensions_A"); // cleaner but optional, since cascading character of css will render --B first anyway at this point
嘗試類似來源:http://www.w3schools.com/jsref/met_document_getelementsbyclassname.asp –
沒有javascript,你可以使用下面的代碼: - .my-nice-class {{0}} max-width:40%!important; 最大高度:40%!重要; } –