Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t将内联css分配给javascript元素_Javascript_Html_Css - Fatal编程技术网

Can';t将内联css分配给javascript元素

Can';t将内联css分配给javascript元素,javascript,html,css,Javascript,Html,Css,我正在构建一个将div注入站点的代码。问题是,我无法通过javascript分配css值,因为它们似乎不会发生在文档上(不是全部) var barRoot = document.createElement('DIV'); barRoot.style.backgroundColor='#44AA44'; barRoot.style['height']='30px'; barRoot.style['width']='100%'; barRoot.style['position']='absolut

我正在构建一个将div注入站点的代码。问题是,我无法通过javascript分配css值,因为它们似乎不会发生在文档上(不是全部)

var barRoot = document.createElement('DIV'); 
barRoot.style.backgroundColor='#44AA44';
barRoot.style['height']='30px';
barRoot.style['width']='100%';
barRoot.style['position']='absolute';
barRoot.style['font-size']='14px';
barRoot.style['font-family']='Arial, Helvetica, sans-serif';
barRoot.style['z-index']='99999001';
barRoot.style['font-weight']='bold';
barRoot.style['top']='0pt';
barRoot.style['left']='0pt';
barRoot.style['color']='White';
barRoot.style['padding']='0pt';
barRoot.style['margin']='0pt';
barRoot.style['border']='0px solid rgb(0, 0, 0)';
barRoot.id =  'irobRootElem1';  
document.body.insertBefore(barRoot, document.body.firstChild);  

var heartImg = document.createElement('IMG');   
heartImg.src = 'heart.png';  
heartImg.style['float']='left';  
heartImg.style['margin-left']='8px';  
heartImg.style['margin-top']='5px';  
heartImg.style['margin-right']='8px';  
barRoot.appendChild(heartImg);  
代码如下:

var barRoot = document.createElement('DIV'); 
barRoot.style.backgroundColor='#44AA44';
barRoot.style['height']='30px';
barRoot.style['width']='100%';
barRoot.style['position']='absolute';
barRoot.style['font-size']='14px';
barRoot.style['font-family']='Arial, Helvetica, sans-serif';
barRoot.style['z-index']='99999001';
barRoot.style['font-weight']='bold';
barRoot.style['top']='0pt';
barRoot.style['left']='0pt';
barRoot.style['color']='White';
barRoot.style['padding']='0pt';
barRoot.style['margin']='0pt';
barRoot.style['border']='0px solid rgb(0, 0, 0)';
barRoot.id =  'irobRootElem1';  
document.body.insertBefore(barRoot, document.body.firstChild);  

var heartImg = document.createElement('IMG');   
heartImg.src = 'heart.png';  
heartImg.style['float']='left';  
heartImg.style['margin-left']='8px';  
heartImg.style['margin-top']='5px';  
heartImg.style['margin-right']='8px';  
barRoot.appendChild(heartImg);  

如果希望通过JavaScript指定样式,则属性名称与CSS文件中的名称不完全相同

var barRoot = document.createElement('DIV'); 
barRoot.style.backgroundColor='#44AA44';
barRoot.style['height']='30px';
barRoot.style['width']='100%';
barRoot.style['position']='absolute';
barRoot.style['font-size']='14px';
barRoot.style['font-family']='Arial, Helvetica, sans-serif';
barRoot.style['z-index']='99999001';
barRoot.style['font-weight']='bold';
barRoot.style['top']='0pt';
barRoot.style['left']='0pt';
barRoot.style['color']='White';
barRoot.style['padding']='0pt';
barRoot.style['margin']='0pt';
barRoot.style['border']='0px solid rgb(0, 0, 0)';
barRoot.id =  'irobRootElem1';  
document.body.insertBefore(barRoot, document.body.firstChild);  

var heartImg = document.createElement('IMG');   
heartImg.src = 'heart.png';  
heartImg.style['float']='left';  
heartImg.style['margin-left']='8px';  
heartImg.style['margin-top']='5px';  
heartImg.style['margin-right']='8px';  
barRoot.appendChild(heartImg);  
检查

var barRoot = document.createElement('DIV'); 
barRoot.style.backgroundColor='#44AA44';
barRoot.style['height']='30px';
barRoot.style['width']='100%';
barRoot.style['position']='absolute';
barRoot.style['font-size']='14px';
barRoot.style['font-family']='Arial, Helvetica, sans-serif';
barRoot.style['z-index']='99999001';
barRoot.style['font-weight']='bold';
barRoot.style['top']='0pt';
barRoot.style['left']='0pt';
barRoot.style['color']='White';
barRoot.style['padding']='0pt';
barRoot.style['margin']='0pt';
barRoot.style['border']='0px solid rgb(0, 0, 0)';
barRoot.id =  'irobRootElem1';  
document.body.insertBefore(barRoot, document.body.firstChild);  

var heartImg = document.createElement('IMG');   
heartImg.src = 'heart.png';  
heartImg.style['float']='left';  
heartImg.style['margin-left']='8px';  
heartImg.style['margin-top']='5px';  
heartImg.style['margin-right']='8px';  
barRoot.appendChild(heartImg);  
但正如所建议的那样,使用比编程设置样式更容易使用

var barRoot = document.createElement('DIV'); 
barRoot.style.backgroundColor='#44AA44';
barRoot.style['height']='30px';
barRoot.style['width']='100%';
barRoot.style['position']='absolute';
barRoot.style['font-size']='14px';
barRoot.style['font-family']='Arial, Helvetica, sans-serif';
barRoot.style['z-index']='99999001';
barRoot.style['font-weight']='bold';
barRoot.style['top']='0pt';
barRoot.style['left']='0pt';
barRoot.style['color']='White';
barRoot.style['padding']='0pt';
barRoot.style['margin']='0pt';
barRoot.style['border']='0px solid rgb(0, 0, 0)';
barRoot.id =  'irobRootElem1';  
document.body.insertBefore(barRoot, document.body.firstChild);  

var heartImg = document.createElement('IMG');   
heartImg.src = 'heart.png';  
heartImg.style['float']='left';  
heartImg.style['margin-left']='8px';  
heartImg.style['margin-top']='5px';  
heartImg.style['margin-right']='8px';  
barRoot.appendChild(heartImg);  
以下是从上面的w3链接中提取的列表:

var barRoot = document.createElement('DIV'); 
barRoot.style.backgroundColor='#44AA44';
barRoot.style['height']='30px';
barRoot.style['width']='100%';
barRoot.style['position']='absolute';
barRoot.style['font-size']='14px';
barRoot.style['font-family']='Arial, Helvetica, sans-serif';
barRoot.style['z-index']='99999001';
barRoot.style['font-weight']='bold';
barRoot.style['top']='0pt';
barRoot.style['left']='0pt';
barRoot.style['color']='White';
barRoot.style['padding']='0pt';
barRoot.style['margin']='0pt';
barRoot.style['border']='0px solid rgb(0, 0, 0)';
barRoot.id =  'irobRootElem1';  
document.body.insertBefore(barRoot, document.body.firstChild);  

var heartImg = document.createElement('IMG');   
heartImg.src = 'heart.png';  
heartImg.style['float']='left';  
heartImg.style['margin-left']='8px';  
heartImg.style['margin-top']='5px';  
heartImg.style['margin-right']='8px';  
barRoot.appendChild(heartImg);  
文档对象模型(DOM)级别2样式规范 版本1.0 W3C建议13,2000年11月
我会添加一个样式标签。用于设置内联样式。

您想要的结果是什么?现在绿色背景不会封装图像,因为图像向左浮动,没有高度:“不是所有的”:哪一个?这可能是有原因的。您是否考虑过只向插入的元素添加一个类,并在样式表中声明该类以及所有需要的设置。对我来说,这似乎是一个简单而有效的方法…谢谢大家,我后来确实上过课,但我只是想知道为什么它不起作用,它把我吓坏了
var barRoot = document.createElement('DIV'); 
barRoot.style.backgroundColor='#44AA44';
barRoot.style['height']='30px';
barRoot.style['width']='100%';
barRoot.style['position']='absolute';
barRoot.style['font-size']='14px';
barRoot.style['font-family']='Arial, Helvetica, sans-serif';
barRoot.style['z-index']='99999001';
barRoot.style['font-weight']='bold';
barRoot.style['top']='0pt';
barRoot.style['left']='0pt';
barRoot.style['color']='White';
barRoot.style['padding']='0pt';
barRoot.style['margin']='0pt';
barRoot.style['border']='0px solid rgb(0, 0, 0)';
barRoot.id =  'irobRootElem1';  
document.body.insertBefore(barRoot, document.body.firstChild);  

var heartImg = document.createElement('IMG');   
heartImg.src = 'heart.png';  
heartImg.style['float']='left';  
heartImg.style['margin-left']='8px';  
heartImg.style['margin-top']='5px';  
heartImg.style['margin-right']='8px';  
barRoot.appendChild(heartImg);