从javascript(不是单个样式参数)将整个样式字符串设置为元素 [英] setting a whole style string to an element from javascript (not individual style parameters)
问题描述
我需要通过javascript将任意样式设置为span。
I need to set an arbitrary style to a span via javascript.
我知道我可以这样做:span.style.height =250px;
但是我需要能够通过模板插入随机完整样式定义
I know I can do things like: span.style.height="250px"; But I need to be able to insert a random full style definition from a template
例如
float:left;
text-decoration:underline;
cursor:pointer;
color:blue;
在Javascript中有办法吗?类似于element.style =all style definition here; ?
Is there a way to do that in Javascript? Something like element.style=" all style definition here"; ?
推荐答案
.style.cssText属性如何? 这里是Microsoft的解释。
How about the .style.cssText property? Here's Microsoft's explanation.
投入你想要应用的样式:
Throw it the styles you'd like to apply like so:
document.getElementById('myEl').style.cssText = 'float:left;margin-top:75px;';
至于浏览器支持,虽然它是IE专有的,我相信它是支持IEs,FF3和Safari 3.2 WIN)。
As for browser support, although it was IE-proprietary I believe it's well-supported (works in the IEs, FF3, and Safari 3.2 WIN at least).
这篇关于从javascript(不是单个样式参数)将整个样式字符串设置为元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!