2017-02-19 142 views
0

無論如何,我可以動態設置樣式屬性到對象屬性嗎?我嘗試了下面的代碼,並得到一個錯誤。如何將動態樣式屬性設置爲對象屬性

var page = {showElement:'none'}; 

var markup='<div id="button"> </div><style>#button{display:'+page.showElement+';}</style>'; 

var mainDiv = document.getElementById('mainDiv'); 
    mainDiv.innerHTML = markup; 
+0

使用JavaScript它'ElementAnyWayYouWantToGetIt.style.display =' – PHPglue

回答

1

您已經有了div元素的ID,爲什麼不使用它來更改顯示屬性?

document.getElementById('button').style.diplay = 'none'; 

document.getElementById('button').style.diplay = page.showElement; 

所以你並不真正需要使用:

mainDiv.innerHTML 
+0

這工作,只是想看看是否有是任何可以動態設置樣式屬性的方法。 –