js学习之document对象学习

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>document对象学习</title>
		<!--
			document对象学习:
				1、document对象的概念
					浏览器对外提供的支持js的用来操作HTML文档的一个对象,此对象封存的HTML文档的所有信息。
				2、使用document
					获取HTML元素对象
						直接获取方式:
							通过id
							通过name属性值
							通过标签名
							通过class属性值
						间接获取方式:
							父子关系
							子父关系
							兄弟关系
		-->
		<!--声明js代码域-->
		<script type="text/javascript">
				//document获取元素对象
					//直接方式
						//id方式
						function testGetEleById(){
							var inp=window.document.getElementById("uname");
							alert(inp);
						}	
						//name方式
						function testGetEleByName(){
							var favs=document.getElementsByName("fav");
							alert(favs);
						}	
						//标签名
						function testGetEleByTagName(){
							var inps=document.getElementsByTagName("input");
							alert(inps);
						}
						//class属性
						function testGetEleByClassName(){
							var inps=document.getElementsByClassName("common");
							alert(inps.length);
						}
					//间接获取方式
						//父子关系
							function testParent(){
								//获取父级元素对象
								var showdiv=document.getElementById("showdiv");
								//获取所有的子元素对象数组
								var childs=showdiv.childNodes;
								alert(childs.length);
							}
						//子父关系
							function testChild(){
								//获取子元素对象
									var inp=document.getElementById("inp");
									var div=inp.parentNode;
									alert(div);
							}
						//兄弟关系
							function testBrother(){
								
									var inp=document.getElementById("inp");
									var preEle= inp.previousSibling;//弟获取兄
									var nextEle=inp.nextSibling;//兄获取弟
									alert(preEle+":::"+nextEle);
								
							}
						
		</script>
		<style type="text/css">
			.common{}
			#showdiv{
				border: solid 2px orange;
				width: 300px;
				height: 300px;
			}
		</style>
	</head>
	<body>
		<h3>document对象的概念和获取元素对象学习</h3>
		直接获取方式学习:<br />
		<input type="button" name="" id="" value="测试获取HTML元素对象--id" onclick="testGetEleById()" />
		<input type="button" name="" id="" value="测试获取HTML元素对象---name" onclick="testGetEleByName()" />
		<input type="button" name="" id="" value="测试获取HTML元素对象---TagName" onclick="testGetEleByTagName()" />
		<input type="button" name="" id="" value="测试获取HTML元素对象---className" onclick="testGetEleByClassName()" />
		<hr />
			用户名:<input type="text" name="uname" id="uname" value="" /><br /><br />
			<input type="checkbox" name="fav" id="fav" value="" class="common"/>唱歌
			<input type="checkbox" name="fav" id="fav" value="" class="common"/>跳舞
			<input type="checkbox" name="fav" id="fav" value="" />睡觉
			<input type="checkbox" name="fav" id="fav" value="" />打游戏
		<hr />
		间接获取方式学习:<br />
		<input type="button" name="" id="" value="测试父子关系"  onclick="testParent()"/>
		<input type="button" name="" id="" value="测试子父关系"  onclick="testChild()"/>
		<input type="button" name="" id="" value="测试兄弟关系"  onclick="testBrother()"/>
		<hr />
		<div id="showdiv"><input type="" name="" id="" value="" /><input type="" name="" id="inp" value="" />
			<input type="" name="" id="" value="" />
			<input type="" name="" id="" value="" />
			<input type="" name="" id="" value="" />
			<input type="" name="" id="" value="" />
		</div>
			
			
			
			
	</body>
</html>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

playboy-jordan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值