javascript 修改 CSS

by on # javascript

JavaScript 可透過下面方式修改 CSS

function change(eve) {
    var css = '';
    switch (eve) {
        case 'hover':
            css = 'p:hover { color: red; }';
            break;
        default:
            css = 'p:hover { color: black; }';
            break;
    }
    var style = document.createElement('style');

    if (style.styleSheet) {
        style.styleSheet.cssText = css;
    } else {
        style.appendChild(document.createTextNode(css));
    }

    document.getElementsByTagName('head')[0].appendChild(style);
}

下面我在CodePen有做一個範例,可參考。

javascript, CSS
comments powered by Disqus