<script id="skinTemplate" type="text/plain"> .text,a.text { color: <%=window.textColor%>; } .window_current .text,.window_current a.text { color: <%=currentWindow.textColor%>; } .titleText, a.titleText { color: <%=window.titleColor%>; font-weight: <%=window.titleFontWeight%>; } .window_current .titleText,.window_current a.titleText { color: <%=currentWindow.titleColor%>; } </script>
这段代码应该是CSS代码,但是是写在script内的,且使用了<%=***%>的赋值,它是如何获取值并转化为页面的CSS的?
你可以参考jquery的template相关函数实现,是完全一个道理。
<script>
在html里默认的不可见元素,你可以把它看成一个普通的dom元素,在用document.getElementById('skinTemplate').innerHTML
获取它的内容后,将里面的<%=...%>
用正则替换成相应的值,然后将它set到一个style元素里,最后append到body里去。