现在的位置: 首页 > web前端 > 正文

JSdocument对象简单用法完整示例

2020年02月18日 web前端 ⁄ 共 3180字 ⁄ 字号 评论关闭

本文实例讲述了JS document对象简单用法。分享给大家供大家参考,具体如下:

<html> <head> <title>js-document对象学习</title> <meta charset="UTF-8"/> <script type="text/javascript">// 直接获取对象 function testGetElementById(){ //通过id获取对象// var gby=window.document.getElementById(); //window可以省去不写 var gby=document.getElementById("sid"); alert(gby); //输出的返回值是标签的类型[object HTMLInputElement] } function testGetElementsByName(){ //通过name获取对象 var gbn=document.getElementsByName("umane"); alert(gbn); //输出的返回值类型是[object NodeList]一个对象类型的数组 alert(gbn.length); //Nodelist的元素是节点元素,长度是节点的个数。每一个容器或标签算是一个节点。 } function testGetElementsByTagName(){ //通过TagName(标签)获取对象 var gbt=document.getElementsByTagName("input"); alert(gbt); //输出返回值类型是[object HTMLCollection]是一个对象元素的集合 alert(gbt.length); //其集合的数目是所有input个数 } function testGetElementsByClassName(){ //通过class获取对象 var gbc=document.getElementsByClassName("clname"); alert(gbc); //输出返回值类型是[object HTMLCollection]是一个对象元素的集合 alert(gbc.length); //集合元素的长度是含有传入类属性的元素个数。 }// 间接获取对象 function testParent(){ //利用父节点调用其节点对象 var showp=document.getElementById("showp"); var tchild=showp.childNodes; alert(tchild); //输出返回值类型是[object NodeList]的一个list数组 alert(tchild.length); //返回子节点的长度 13,是由于在p中和text有换行符算一个子节点 } function testChild(){ //利用子节点调用其父节点 var showp=document.getElementById("child"); var tparent=showp.parentNode; alert(tparent); //输出返回值类型是[object HTMLDivElement](其父节点的类型) } function testBorther(){ //利用子节点调用其父节点 var showp=document.getElementById("target"); var topBorther=showp.previousSibling; //输出参考对象上面的元素 var lowBorther=showp.nextSibling //输出参考元素的下面的元素 alert(topBorther+":::"+lowBorther); //输出返回值类型是[object HTMLDivElement](其父节点的类型) } </script> <style type="text/css"> .clname{} #showp{ border: solid 2px cyan; width: 300px; height: 400px; } input[type=text]{ margin-left: 3px; } </style> </head> <body> <h3>js-document对象学习</h3> <h4>直接调用</h2> <input type="button" name="" id="sid" value="测试GetElementById" onclick="testGetElementById()" /> <input type="button" name="umane" id="" value="测试GetElementByName" onclick="testGetElementsByName()" /> <input type="button" name="" id="" value="测试GetElementsByTagNames" class="clname" onclick="testGetElementsByTagName()" /> <input type="button" name="" id="" value="测试GetElementsByClassName" class="clname" onclick="testGetElementsByClassName()" /> <hr /><br /> <h4>间接调用</h2> <input type="button" name="" id="" value="测试Parent" onclick="testParent()" /> <input type="button" name="" id="" value="测试 Child" onclick="testChild()" /> <input type="button" name="" id="" value="测试Borther" onclick="testBorther()" /> <p id="showp"> <input type="text" name="" id="parent" value="" /> <input type="text" name="" id="child" value="" /> <input type="text" name="" id="target" value="" /> <input type="text" name="" id="" value="" /> <input type="text" name="" id="" value="" /> <input type="text" name="" id="" value="" /> </p> </body></html>

运行结果:

Document:

获取HTML元素:

1:直接获取方式:通过id 通过name属性值 通过标签名 通过class属性值

2:间接获取方式:父子关系 子父关系 兄弟关系

3:操作HTML元素对象的属性

操作HTML元素对象的内容和样式

操作HTML的文档结构

document操作Form元素

document操作表格

document对象实现form表单校验

感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools.xuebuyuan.com/code/HtmlJsRun测试上述代码运行效果。

更多关于JavaScript相关内容可查看本站专题:《JavaScript操作DOM技巧总结》、《JavaScript页面元素操作技巧总结》、《JavaScript事件相关操作与技巧大全》、《JavaScript查找算法技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript错误与调试技巧总结》

希望本文所述对大家JavaScript程序设计有所帮助。

以上就上有关JSdocument对象简单用法完整示例的相关介绍,要了解更多document对象内容请登录学步园。

抱歉!评论已关闭.