简体   繁体   English

使用javascript更改css中所有出现的单词

[英]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修改样式表规则选择器:

Fiddle 小提琴

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.

相关问题 更改特定词(和所有出现的词)的颜色 - Change SPECIFIC word (and all occurrences) color Javascript:查找文本文档中出现的所有单词 - Javascript: find all occurrences of word in text document 使用CSS,JavaScript或jQuery在悬停时更改单词? - Word change on hover using CSS, JavaScript, or jQuery? 使用Match使用Javascript计算字符串中单词的出现次数 - Counting occurrences of a word in a string with Javascript using match 更改所有出现的字符的颜色 javascript/reactjs - Change color of all occurrences of a character javascript/reactjs 在 JavaScript 中为字符串中所有出现的单词添加前缀和后缀 - Add prefix and suffix to all occurrences of a word in a string in JavaScript 如何使用Javascript或jQuery突出显示页面上所有单词的出现? - How to highlight all occurrences of a word on a page with Javascript or jQuery? 使用javascript替换字符串中的所有匹配项 - replace all occurrences in string using javascript 除非使用正则表达式将字符包含在单词中,否则如何替换所有出现的字符 - How to replace all occurrences of a character unless it is included in a word using regex 如何使用 javascript 计算字符串数组中单词的出现次数? - How can I count the occurrences of a word in an array of strings using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM