js document用法

2025-04-19 01:24:12

1、使用sublime text2编写html文件

2、document的相关方法:getElementById() 返回对拥有指定 id 的第一个对象的引用。g髫潋啜缅etElementsByName() 返回带有指定名称的对象集合。getElementsByTagName() 返回带有指定标签名的对象集合。

3、内容:<!DOCTYPE html><html><head><meta http-equiv=&鳎溻趄酃quot;Content-Type" content="text/html; charset=utf-8" /> <script>function changeLink(){// document.getElementById('myAnchor').innerHTML="22";// //document.getElementById('myAnchor').outerHTML="longlong";// document.getElementById('myAnchor').href="http://www.w3school.com.cn";// document.getElementById('myAnchor').target="_blank";// var myAnch = document.getElementById('myAnchor');// alert(myAnch.innerHTML); //output: 微软// alert(myAnch.outerHTML); //output: <a id="myAnchor" href="http://www.microsoft.com">微软</a> // alert(document.innerHTML); //output: undefined// alert(document.outerHTML); //output: undefined// alert(document.body.innerHTML); //output: /*<a id="myAnchor" href="http://www.microsoft.com">微软</a><input type="button" onclick="changeLink()" value="更改链接">*/ // alert(document.body.outerHTML); //output: 整个body/*<body><a id="myAnchor" href="http://www.microsoft.com">微软</a><input onclick="changeLink()" value="更改链接" type="button"></body>*/// alert(document.html.innerHTML); //output: 不会显示任何东西// alert(document.html.outerHTML); //output: 不会显示任何东西 var totalContent=document.getElementsByTagName('html')[0].innerHTML; alert(totalContent); //output: head和body的内容}</script></head><body><a id="myAnchor" href="http://www.microsoft.com">微软</a><input type="button" onclick="changeLink()" value="更改链接"></body></html>

4、将网页放到浏览器中,点击按钮,结果:

js document用法
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢