我是编程新手,对带有id属性的HTML元素有疑问。我的问题是 - 如果一个HTML元素有一个id属性,id属性的值是否可用,而不需要在JavaScript中定义它呢?这里是让这个更清楚的例子:是否已经在JavaScript中定义了id的html元素?
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<input type="checkbox" id="mycheckbox" checked />
<input type="radio" id="myradio" checked />
<p id="test">
p tag too.
</p>
<script>
if(mycheckbox.checked) {
alert("How does this work? Are there more pre-defined stuff (varaibles?) like this one?");
}
if(myradio.checked) {
alert("Another example with radio");
}
alert(test.innerHTML);
</script>
</body>
我与HTML元素的工作阅读JavaScript的一些书籍,并没有给这个快捷方式(如果它是一个)。
预先感谢您。
这可能是因为旧黑暗时代向后兼容性的作品时,IE6是浏览器,但不推荐。使用'document.getElementById()'而不是 – Peter
http://stackoverflow.com/questions/3434278/do-dom-tree-elements-with-ids-become-global-variables – AmmarCSE
我建议你阅读[DOM]( https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction)了解HTML和javascript之间的链接 – Ander2