今日、私はJavascriptのDOM要素にそのIDだけでアクセスする可能性を見つけました。IDで直接DOM要素にアクセスする際の欠点は何ですか?
elementid.style.backgroundColor = "blue"
IE、Firefox、Chromeで動作するかどうかは、非常に短いスニペットでテストしました。ダイレクトアクセスの欠点が何であるかと($()同様または類似のフレームワークの交換)document.getElementById('elementid')
を使用すべき場合に
<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>
は、だから私は疑問に思っ: は、ここで私が使用しスニペットです。
これに関する有用な文書は見つかりませんでした。どこでもgetElementById
またはフレームワークメソッドが使用されます。
のようなIDを使用することはできません(2)。 –