是否可以引入特定于 id 的样式或根据您所在的“url”加载不同的 css 文件?
像这样:
<script>
if(location.href == 'http://jpftest2.tumblr.com/about'){
document.write('<style type='text/css'>
.social {
display:none;
}
</style>');
}
</script>
所以当用户离开根 url 到“/about”页面时。我想介绍一下风格:
.social {
display:none;
}
如果还可以根据 URL 完全加载不同的 CSS 文件会更好吗?
tumblr 上的不同页面共享相同的头部,所以我不能在不同页面上引入不同的 css
最佳答案
假设您的网站正在使用类似 AJAX 的东西(正确吗?),(否则您会将样式表放在文件的 HTML <head>
中),您可以这样:
window.onpopstate = function() {
if(window.location.path == '/about'){
var css = document.createElement('style');
css.type = 'text/css';
css.rel = 'stylesheet';
css.innerHTML = '.social{display:none;}';
document.getElementsByTagName('head')[0].appendChild(css);
}
}
但是我不推荐它。
关于javascript - 使用 jquery 或其他工具根据 URL 加载不同的 css 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13151627/