2014-06-09 22 views
0

在我的CSS我有一个div功能,增加了一类特殊的一个div

div.videl 
{ 
    width: 80%; 
    margin: 0 auto; 
    background-color: #39275b; 
    color: white; 
    padding: 5px; 
} 

,然后一个功能的特定类添加该类div S:

this.addVideo = function() 
{ 
     var newVidElement = document.createElement("div"); 
     newVidElement.class = "videl"; 
     newVidElement.innerHTML = "<p>(" + ++this.numVids + ") <textarea class='vidtxt'></textarea></p>" 
     document.getElementById("vidplaydiv").appendChild(newVidElement); 
} 

然而,出于某种原因,该功能在我测试时未正确应用CSS属性。看到这个页面http://jaminweb.com/YoutubePlaylist.html并点击Add Another Video按钮。任何想法我做错了什么?

+0

我怀疑纯dom元素没有任何'class'属性,它应该是'className',请注意HTML中的属性和DOM中的属性之间并不总是对应的。 –

回答

3

className是您尝试设置的属性的名称。

newVidElement.className = "videl"; 
0

更改JavaScript代码如下 -

this.addVideo = function() 
{ 
    var newVidElement = document.createElement("div"); 
    newVidElement.className = "videl"; 
    newVidElement.innerHTML = "<p>(" + ++this.numVids + ") <textarea class='vidtxt'></textarea></p>" 
    document.getElementById("vidplaydiv").appendChild(newVidElement); 
} 
1

当你不知道HTML属性的属性名称,您可以随时使用的setAttribute,例如:

newVidElement.setAttribute('class','videl')