首页 > 开发 > JS > 正文

web qq的一段代码请教

2017-09-05 13:02:53  来源: 网友分享
<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里去。