如何将动态样式属性设置为对象属性
问题描述:
无论如何,我可以动态设置样式属性到对象属性吗?我尝试了下面的代码,并得到一个错误。如何将动态样式属性设置为对象属性
var page = {showElement:'none'};
var markup='<div id="button"> </div><style>#button{display:'+page.showElement+';}</style>';
var mainDiv = document.getElementById('mainDiv');
mainDiv.innerHTML = markup;
答
您已经有了div元素的ID,为什么不使用它来更改显示属性?
document.getElementById('button').style.diplay = 'none';
或
document.getElementById('button').style.diplay = page.showElement;
所以你并不真正需要使用:
mainDiv.innerHTML
+0
这工作,只是想看看是否有是任何可以动态设置样式属性的方法。 –
使用JavaScript它'ElementAnyWayYouWantToGetIt.style.display =' – PHPglue