我只能访问CMS的后端。他们这样加载自定义样式表如何使用JavaScript定位CSS链接
<link rel="stylesheet" media="(min-width:481px)" href=".../css/custom/styles.css" type="text/css">
我需要删除media="(min-width:481px)"
。有没有办法做到这一点与JavaScript(我不认为CMS使用jQuery)?
我只能访问CMS的后端。他们这样加载自定义样式表如何使用JavaScript定位CSS链接
<link rel="stylesheet" media="(min-width:481px)" href=".../css/custom/styles.css" type="text/css">
我需要删除media="(min-width:481px)"
。有没有办法做到这一点与JavaScript(我不认为CMS使用jQuery)?
我个人不建议用JavaScript的一个链接的元素搞乱,但如果你仍然想这样做,这应该工作:
document.querySelector('link[href=href=".../css/custom/styles.css"]').removeAttribute('media');
如果您仍然想这样做,一定要等待CSS样式加载。
如果你想要去的这个解决方案出于某种原因,并做了所有的链接标签,那么这一个很简单:
var linkList = document.querySelectorAll('link');
for(var i in linkList) {
linkList[i].removeAttribute('media');
}
始终使用.removeAttribute
,而不是使用setAttribute
的参数设置为null
。这通常是不好的做法。
当然有一种方法。它是否真的会做任何事情都是另一回事。在JavaScript有机会加载之前,样式表将被加载(或不加载)。只需通过JavaScript加载样式表,而不是搞乱现有的'link'元素,可能会更好。为此,在SO上有很多答案,包括:http://stackoverflow.com/q/9979415/215552 –
那么,什么样的CMS,只是解决后端问题会更容易。 – adeneo
我试过联系CMS提供商,他100%确定删除'media =“(min-width:481px)”'不能完成。 – Heather