我有一个需求,根据条件,我需要呈现一个样式表。
<script type="text/javascript">
var isWelcomePage = window.location.pathname.match(/^\welcome/);
if(isWelcomePage){
<link rel="stylesheet" type="text/css" href="welcome.css"/>
}
</script>这个是可能的吗?还是有更好的方法?
发布于 2013-10-02 08:40:16
是的,试试这个
HTML
<link rel="stylesheet" type="text/css" href="" id="updatable-css" />JAVASCRIPT
var isWelcomePage = window.location.pathname.match(/^\welcome/);
if(isWelcomePage){
document.getElementById('updatable-css').href = "welcome.css";
}发布于 2013-10-02 08:50:03
您可以动态创建link标记,尝试如下:
<script type="text/javascript">
var isWelcomePage = window.location.pathname.match(/^\welcome/);
if(isWelcomePage) {
var link = document.createElement('link');
link.href = 'welcome.css';
link.rel = 'stylesheet';
link.type = 'text/css';
document.head.appendChild(link);
}
</script>https://stackoverflow.com/questions/19133008
复制相似问题