javascript - 无法将内联 css 分配给 javascript 元素

标签 javascript html css

我正在构建一个将 div 注入(inject)网站的代码。问题是我无法通过 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);  

最佳答案

如果您想通过 JavaScript 分配样式,属性名称与您的 CSS 文件中的名称不完全相同。

检查 this list in the w3c doc

但如建议的那样,使用比以编程方式设置样式更易于使用

这是从上面的 w3 链接中提取的列表:

Document Object Model (DOM) Level 2 Style Specification

Version 1.0

W3C Recommendation 13 November, 2000

azimuth
background
backgroundAttachment
backgroundColor
backgroundImage
backgroundPosition
backgroundRepeat
border
borderCollapse
borderColor
borderSpacing
borderStyle
borderTop
borderRight
borderBottom
borderLeft
borderTopColor
borderRightColor
borderBottomColor
borderLeftColor
borderTopStyle
borderRightStyle
borderBottomStyle
borderLeftStyle
borderTopWidth
borderRightWidth
borderBottomWidth
borderLeftWidth
borderWidth
bottom
captionSide
clear
clip
color
content
counterIncrement
counterReset
cue
cueAfter
cueBefore
cursor
direction
display
elevation
emptyCells
cssFloat
font
fontFamily
fontSize
fontSizeAdjust
fontStretch
fontStyle
fontVariant
fontWeight
height
left
letterSpacing
lineHeight
listStyle
listStyleImage
listStylePosition
listStyleType
margin
marginTop
marginRight
marginBottom
marginLeft
markerOffset
marks
maxHeight
maxWidth
minHeight
minWidth
orphans
outline
outlineColor
outlineStyle
outlineWidth
overflow
padding
paddingTop
paddingRight
paddingBottom
paddingLeft
page
pageBreakAfter
pageBreakBefore
pageBreakInside
pause
pauseAfter
pauseBefore
pitch
pitchRange
playDuring
position
quotes
richness
right
size
speak
speakHeader
speakNumeral
speakPunctuation
speechRate
stress
tableLayout
textAlign
textDecoration
textIndent
textShadow
textTransform
top
unicodeBidi
verticalAlign
visibility
voiceFamily
volume
whiteSpace
widows
width
wordSpacing
zIndex

关于javascript - 无法将内联 css 分配给 javascript 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11815766/

相关文章:

html - html中<p>标签的使用

javascript - 按 Javascript 中的不同元素排序

javascript - 当用户单击浏览器操作并出现弹出窗口时如何执行操作(Chrome 扩展)

javascript - 如何重写javascript模板字符串

javascript - 为什么我总是得到 NaN?

html - 有谁知道播放 m3u8 视频文件的嵌入式视频播放器?

html - Google 字体无法在我的网站上使用

html - 如何将列表项与 svg 多边形对齐?

html - 使图像高度适合屏幕

使用显示 block 和 100% 宽度属性时 HTML 表格 td 不是全宽