如何将css添加到Javascript中?
假设这是我的javascript代码:如何将css添加到Javascript中?,javascript,jquery,html,css,Javascript,Jquery,Html,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 =
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);
现在我想在bottomChild中添加以下css:
::-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分区中。您应该将想要的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';
您应该将您想要的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';
您可以像这样添加类-
div(id)代码>
在您的情况下,添加:
bottomChild.className = 'target';
Js:
Css:
工作小提琴:您可以像这样添加类-div(id)代码>
在您的情况下,添加:
bottomChild.className = 'target';
Js:
Css:
工作小提琴:将类规则添加到CSS文件中,然后使用javascript设置它们是更好的方法,尽管有时这是不可行的
下面是如何动态添加样式
注意,由于内联样式比外部CSS规则具有更高的特殊性,因此需要使用!重要信息
覆盖样式,如下面的示例所示
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滚动条”,“宽度:22px;”)
addRule('#bottomChildId::-webkit滚动条轨迹','-webkit框阴影:插入0.06px rgba(0,0,0,0.3);-webkit边框半径:10px;边框半径:10px;'))
//请注意这是如何不起作用的,因为内联样式具有更高的特殊性
addRule(“#bottomChildId”,“背景:蓝色”)
//!重要信息:必须覆盖内联样式
addRule(“#bottomChildId”,“宽度:200px!重要”)
}
函数addRule(s,r){
如果(!hasStyleSheet(文件))返回;
var ss=document.styleSheets[document.styleSheets.length-1];
ss.insertRule(s+'{'+r+'}',ss.cssRules.length);
功能表(d){
如果(!d.styleSheets)返回false;
如果(!d.styleSheets.length){
var style=document.createElement(“样式”);
//style.setAttribute(“媒体”、“屏幕”)
style.appendChild(d.createTextNode(“”);//Webkit需要这个
d、 头。附属物(样式);
}
返回true;
}
};代码>将类规则添加到CSS文件中,然后使用javascript设置它们是更好的方法,尽管有时这是不可行的
下面是如何动态添加样式
注意,由于内联样式比外部CSS规则具有更高的特殊性,因此需要使用!重要信息
覆盖样式,如下面的示例所示
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滚动条”,“宽度:22px;”)
addRule('#bottomChildId::-webkit滚动条轨迹','-webkit框阴影:插入0.06px rgba(0,0,0,0.3);-webkit边框半径:10px;边框半径:10px;'))
//请注意这是如何不起作用的,因为内联样式具有更高的特殊性
addRule(“#bottomChildId”,“背景:蓝色”)
//!重要信息:必须覆盖内联样式
addRule(“#bottomChildId”,“宽度:200px!重要”)
}
函数addRule(s,r){
如果(!hasStyleSheet(文件))返回;
var ss=document.styleSheets[document.styleSheets.length-1];
ss.insertRule(s+'{'+r+'}',ss.cssRules.length);
功能表(d){
如果(!d.styleSheets)返回false;
如果(!d.styleSheets.length){
var style=document.createElement(“样式”);
//style.setAttribute(“媒体”、“屏幕”)
style.appendChild(d.createTextNode(“”);//Webkit需要这个
d、 头。附属物(样式);
}
返回true;
}
};代码>文档可能重复。querySelector('style')。textContent+=::-webkit滚动条{width:22px;}:::-webkit滚动条轨迹{-webkit框阴影:插入0 0 6px rgba(0,0,0,0,0.3);-webkit边框半径:10px;边框半径:}
文档可能重复。querySelector('style')).textContent+=::-webkit滚动条{宽度:22px;}::-webkit滚动条轨迹{-webkit框阴影:插入0.6px rgba(0,0,0,0.3);-webkit边框半径:10px;边框半径:10px;}
这是最干净的版本。使用javascript以这种方式进行增强。它还可以带来一个很好的过渡网关。非常感谢~它非常有帮助(x)这是最干净的版本。使用javascript以这种方式进行增强。这也会带来一个很好的过渡通道。非常感谢~这非常有帮助x)谢谢!你刚刚救了我的命谢谢!你刚刚救了我的命