[英]change all occurrences of a word in css using javascript
I've seen we can change all occurrences of text in HTML using javascript using document.body.innerHTML = document.body.innerHTML.replace(/hello/g, 'hi');
我已经看到我们可以使用javascript使用document.body.innerHTML = document.body.innerHTML.replace(/hello/g, 'hi');
更改HTML中所有出现的文本document.body.innerHTML = document.body.innerHTML.replace(/hello/g, 'hi');
but i'm trying to change all occurences inside the CSS file. 但我正在尝试更改CSS文件中的所有出现。
What I'm trying to do is something like that: 我想做的是这样的:
var h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
var w = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var proportion = w/h;
if (proportion > 1.59) {
// width > height
document.body.innerHTML = document.body.innerCSS(?).replace(/vw/g, 'vh');
} else {
// height > width
document.body.innerHTML = document.body.style(?).replace(/vh/g, 'vw');
}
Like changing vw to vh when the width of the page is bigger, and vh to vw when height is bigger. 比如当页面的宽度更大时将vw改为vh,当高度更大时将vh改为vw。
You can modify the stylesheet rule selectors via javascript: 您可以通过javascript修改样式表规则选择器:
function replace_css_selector(search_value, new_value) {
var css_styles = "";
var style = null;
for (var i = 0; i < document.styleSheets.length; i++) {
with(document.styleSheets[i]) {
if (typeof css_rules != "undefined") {
style = css_rules;
} else if (typeof rules != "undefined") {
style = rules;
}
}
for (var item in style) {
if (style[item].cssText != undefined) {
css_styles = (style[item].cssText);
style[item].selectorText = style[item].selectorText.replace(search_value, new_value);
}
}
}
return style;
}
list = replace_css_selector(/vh/g, 'vw');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.