Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        5年前关闭。
                                                                                            
                
        
CSS:

ul #allposts li{
    height: 50px;
    width: 50px;
    background-color: yellow;
    border: 1px solid yellow;
}


HTML:

<ul id = "allposts"></ul>


JS:

var woow2 = document.getElementById('allposts');
var oneofpost = document.createElement('li');
woow2.appendChild(oneofpost);


当我运行代码时,oneofpost不会出现。它应该看起来像是黄色的小砖块,对吗?

有什么想法吗?

最佳答案

问题出在您的CSS中。您的选择器应为:

ul#allposts li


但是ID应该是唯一的,因此您不需要使用标签名称来限定它:

#allposts li


当前的选择器显示为:

所有li元素都是ID为allposts的元素的后代,而这些元素又是所有ul元素的后代,这可能不是您想要的。

关于javascript - 追加不使用JavaScript工作的 child ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21070330/

10-12 00:08
查看更多