今天我偶然發現了在Javascript中訪問DOM元素的可能性。像這樣:通過ID直接訪問DOM元素有什麼缺點?
elementid.style.backgroundColor = "blue"
我測試了一個非常短的代碼片段,如果這在IE,Firefox和Chrome中有效 - 它確實如此。 這裏是我使用的代碼片段:
<html><head>
<script>
function highlight() {
content.style.backgroundColor = "blue";
content.style.color = "white";
}
</script>
</head>
<body>
<div id="content">test content</div>
<div onclick="highlight()">highlight content</div>
</body></html>
所以我想知道在這種情況下document.getElementById('elementid')
應使用(或類似的框架替換像$()),什麼是直接接入的缺點。
我無法找到任何有用的文件。在任何地方使用getElementById
或框架方法。
這並不工作在Firefox(2)。 – 2009-07-08 13:20:53