给定如下结构:
<div class="Stargate" id="MGM">
<div class="SG1">
</div>
<div id="Atlantis">
</div>
</div>
SG1 和亚特兰蒂斯的母公司星际之门或米高梅会是什么?
最佳答案
没有区别,Stargate
和 MGM
都在同一个父 div
上。但是您会发现 MGM
是识别 div
的两种方式中“更好”或“更强”的一种。
“更好/更强”是指 id
将在 CSS/javascript 中产生影响,例如,在 CSS 中,id 更强。
#MGM .SG1 {
/* very strong selector */
}
.Stargate .SG1 {
/* not so strong selector */
}
在 javascript 中你有
var parentDiv = document.getElementById('MGM');
document.getElementById
得到广泛支持,而相应的 document.getElementsByClassName
是现代浏览器中相对较新的补充。
关于subclass - 如果有子元素,则在一个元素上定义 CSS ID 和类,哪一个是父元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6722391/