document. getElementById() //返回对拥有指定 id 的第一个对象的引用
语法: document.getElementById(id)
例:
<html>
<head>
<script type="text/javascript">
function getValue()
{
var x=document.getElementById("myHeader")
alert(x.innerHTML)
}
</script>
</head>
<body>
<h1 id="myHeader" onclick="getValue()">This is a header</h1>
<p>Click on the header to alert its value</p>
</body>
</html>
document.getElementsByName() //返回带有指定名称的对象集合。
语法:document.getElementsByName()
<script>
function getElements()
{
var x=document.getElementsByName("x");
alert(x.length);
}
</script>
<body>
Cats:
<input name="x" type="radio" value="Cats">
Dogs:
<input name="x" type="radio" value="Dogs">
<input type="button" onclick="getElements()" value="How many elements named 'x'?">
</body>
//结果是2
document.getElemenysByTagName() //返回带有指定标签名的对象集合。
innerHTML在JS是双向功能:获取对象的内容 或 向对象插入内容;
document.querySelector() //获取元素对象
function test() {
//获取元素对象
var btn=document.querySelector("input");
//获取属性值
var value=btn.getAttribute("value");
alert(value);
}
getAttribute () //获取属性值
getAttributeNode() //获取属性对象
open() 方法可打开一个新文档,并擦除当前文档的内容。
语法:document.open(mimetype,replace)
参数 描述
mimetype 可选。规定正在写的文档的类型。默认值是 "text/html"。
replace 可选。当此参数设置后,可引起新文档从父文档继承历史条目。
说明:该方法将擦除当前 HTML 文档的内容,开始一个新的文档,新文档用 write() 方法或 writeln() 方法编写。
重要事项:调用 open() 方法打开一个新文档并且用 write() 方法设置文档内容后,必须记住用 close 方法关闭文档,并迫使其内容显示出来。
例:
function createNewDoc()
{
var newDoc=document.open("text/html","replace");
var txt="<html><body>Learning about the DOM is FUN!</body></html>";
newDoc.write(txt);
newDoc.close();
}
</script>
</head>
<body>
<input type="button" value="Write to a new document"
onclick="createNewDoc()">