2016-12-17 57 views
1

比方說這是我的javascript代碼:如何將css添加到Javascript中?

var bottomChild = document.createElement('div'); 
bottomChild.id = 'bottomChildId'; 
bottomChild.style.width = '100px'; 
bottomChild.style.position = 'relative'; 
bottomChild.style.overflowY = 'scroll'; 
bottomChild.style.background = 'red'; 
bottomChild.style.top = '0px'; 
bottomChild.style.borderLeft = '0px solid #E4E7EA'; 
bottomChild.style.height = '200px'; 
document.body.appendChild(bottomChild); 

現在我想下面的CSS添加到bottomChild:

::-webkit-scrollbar { 
    width: 22px; 
} 
::-webkit-scrollbar-track { 
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); 
    -webkit-border-radius: 10px; 
    border-radius: 10px; 
} 

請添加這些CSS成動態JavaScript DIV幫助。

+0

可能重複的顯示在[內聯樣式的CSS僞類](http://stackoverflow.com/questions/5293280/css-pseudo-classes-with-inline-styles) –

+0

document.querySelector('style')。textContent + = ' :: - webkit-scrollbar { width:22px; } :: - webkit-scrollbar-track { -webkit-box-shadow:inset 0 0 6px rgba(0,0,0,0.3); -webkit-border-radius:10px; border-radius:10px; }' –

回答

2

您可以添加類像你的情況這 - div(id).className = 'target';

補充:

bottomChild.className = 'target'; 

JS:

var bottomChild = document.createElement('div'); 
bottomChild.id = 'bottomChildId'; 
bottomChild.style.width = '100px'; 
bottomChild.style.position = 'relative'; 
bottomChild.style.overflowY = 'scroll'; 
bottomChild.style.background = 'red'; 
bottomChild.style.top = '0px'; 
bottomChild.style.borderLeft = '0px solid #E4E7EA'; 
bottomChild.style.height = '200px'; 
bottomChild.className = 'target'; 
document.body.appendChild(bottomChild); 

的CSS:

.target { 
    background:#000000 !important; 
    // etc. 
} 
.target::-webkit-scrollbar { 
    width: 22px; 
} 
.target::-webkit-scrollbar-track { 
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); 
    -webkit-border-radius: 10px; 
    border-radius: 10px; 
} 

工作小提琴:https://jsfiddle.net/mL8g3qLf/

+0

謝謝!你只是救了我的人生xx –

2

您應該將您想要的CSS添加到單獨的類,然後使用javascript將類指定給新元素。

.target { 
    width: 100px; 
    position: relative; 
    // etc. 
} 
.target::-webkit-scrollbar { 
    width: 22px; 
} 
.target::-webkit-scrollbar-track { 
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); 
    -webkit-border-radius: 10px; 
    border-radius: 10px; 
} 

的javascript:

bottomChild.className = 'target'; 
+0

這是最乾淨的版本。使用你的JavaScript來增強這種方式。它也可以成爲一個轉換的好門戶。 – jjeining

+0

非常感謝〜這非常有幫助x) –

0

添加類規則,以你的CSS文件,然後將它們用JavaScript是最好的方式做到這一點,雖然有時這是不可行的。

下面介紹如何動態添加樣式。

注意,因爲內嵌樣式具有比外部CSS規則較高的特異性,一個需要使用!important覆蓋風格,如下面的示例

window.onload = function() { 
 

 
    var bottomChild = document.createElement('div'); 
 
    bottomChild.id = 'bottomChildId'; 
 
    bottomChild.style.width = '100px'; 
 
    bottomChild.style.position = 'relative'; 
 
    bottomChild.style.overflowY = 'scroll'; 
 
    bottomChild.style.background = 'red'; 
 
    bottomChild.style.top = '0px'; 
 
    bottomChild.style.borderLeft = '0px solid #E4E7EA'; 
 
    bottomChild.style.height = '200px'; 
 
    document.body.appendChild(bottomChild); 
 

 
    addRule('#bottomChildId::-webkit-scrollbar', 'width: 22px;') 
 

 
    addRule('#bottomChildId::-webkit-scrollbar-track', '-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); -webkit-border-radius: 10px; border-radius: 10px;') 
 

 
    // Note how this does not work, since inline styles has higher specificity 
 
    addRule('#bottomChildId', 'background: blue') 
 

 
    // !important will be necesarry to override inline style 
 
    addRule('#bottomChildId', 'width: 200px !important') 
 
} 
 

 
function addRule(s, r) { 
 
    if (!hasStyleSheet(document)) return; 
 
    var ss = document.styleSheets[document.styleSheets.length - 1]; 
 
    ss.insertRule(s + ' {' + r + '}', ss.cssRules.length); 
 

 
    function hasStyleSheet(d) { 
 
    if (!d.styleSheets) return false; 
 
    if (!d.styleSheets.length) { 
 
     var style = document.createElement("style"); 
 
     // style.setAttribute("media", "screen") 
 
     style.appendChild(d.createTextNode("")); //Webkit need this 
 
     d.head.appendChild(style); 
 
    } 
 
    return true; 
 
    } 
 
};