簡體   English   中英

使用javascript更改css中所有出現的單詞

[英]change all occurrences of a word in css using javascript

我已經看到我們可以使用javascript使用document.body.innerHTML = document.body.innerHTML.replace(/hello/g, 'hi');更改HTML中所有出現的文本document.body.innerHTML = document.body.innerHTML.replace(/hello/g, 'hi'); 但我正在嘗試更改CSS文件中的所有出現。

我想做的是這樣的:

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');

}

比如當頁面的寬度更大時將vw改為vh,當高度更大時將vh改為vw。

您可以通過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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM