JavaScript访问HTML元素

本文介绍了JavaScript访问HTML元素的各种方式,包括根据ID、CSS选择器、节点关系来访问,以及如何访问表单控件、列表框、下拉菜单选项和表格子元素。详细讲解了document.getElementById、querySelector、querySelectorAll等方法的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

js 动态修改 html 元素,访问 html 元素的方式

1. 根据 ID 访问 html 元素
2. 根据 CSS 选择器访问 html 元素
3. 利用节点关系访问 html 元素

1. 根据ID访问HTML元素

  • document.getElementById( idval ):返回文档中 id 属性值为 idval 的 HTML 元素。
<! DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>id</title>
	<script >
		var accessById = function(){
			alert(document.querySelector("#a").innerHTML +"\n"+document.getElementById("b").value);
		}
	</script >
	
</head>
<body >
	<div id="a">wo shi java</div>
	<textarea id="b" rows="3" cols='25'>javaee</textarea>
	<input type="button" value="访问两个元素" onclick="accessById();">
</body >

结果
在这里插入图片描述

2 - 根据 CSS 选择器访问 html 元素

  • Eelemnt querySelector(Selectos):该方法的参数即可是一个 css 选择器,也可用逗号隔开的多个 css 选择器;该方法返回 html 文档中第一个符合选择器参数的 html 元素。
  • NodeList querySelectorAll(Selectos):该方法与前一个方法类似,只是该方法返回符合 css 选择器的所有 html 元素。
<! DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>,,</title>
	<script >
		var change = function(){
			var divList = document.querySelectorAll("div");
			alert(divList);
			for (var i in divList){
				divList[i].innerHTML = "测试内容"+ i;
				divList[i].style.width = "300px";
				divList[i].style.height = "50px";
				divList[i].style.margin = "10px";
				divList[i].style.backgroundColor = "#faa";
			}
		}
	</script >
	
</head>
<body >
	<div ></div>
	<div ></div>
	<div ></div>
	<input type="button" onclick="change()" value="修改全部div元素">
	
</body >

结果
在这里插入图片描述

3- 利用节点关系访问 html 元素

  • Node parentNode:返回当前节点的父节点,只读属性
  • Node previousSibling:返回当前节点的前一个兄弟节点,只读属性
  • Node nextSibling:返回当前节点的后一个兄弟节点,只读属性
  • Node[ ] childNodes:返回当前节点的所有子节点,只读属性
  • Node[ ] getElementByTagName(tagname):返回当前节点的具有制定标签名的所有子节点。
  • Node firstChild:返回当前节点的第一个节点,只读属性
  • Node lastChild:返回当前节点的最后一个节点,只读属性
<! DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>利用节点关系访问 html 元素</title>
	<style >
		.selected{
			background-color:#66f;
		}
	</style >
	
</head>
<body >
	<ol id="books">
		<li id="java">java1	</li>
		<li id="c">c1</li>
		<li id="ajax" class="selected">c++1</li>
		<li id="python">python1	</li>
		<li id="php">php1</li>
		<li id="go">go1</li>
	</ol>
	<input type="button" value="父节点" onclick="change(curTarget.parentNode);" />
	<input type="button" value="第一个" onclick="change(curTarget.parentNode.firstChild.nextSibling);" />
	<input type="button" value="上一个" onclick="change(curTarget.previousSibling.previousSibling);" />
	<input type="button" value="下一个" onclick="change(curTarget.nextSibling.nextSibling);" />
	<input type="button" value="最后一个" onclick="change(curTarget.parentNode.lastChild.previousSibling);" />
	<script type="text/javascript">
		var curTarget = document.getElementById("ajax");
		var change = function(target){
			alert(target.innerHTML);
		}
	</script>
</body >

结果
在这里插入图片描述

4- 访问表单控件

属性1介绍
action返回表单的action属性,读写属性
elements返回表单内全部表单控件组成的数组
length返回表单内单域的个数
method返回表单的method属性
target用于确定提交表单时的结果窗口,可以是_self 、_parent 、_top 、_blank的值
reset()重设表单
submit()提交表单
<! DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>访问表单控件</title>

	
</head>
<body >
	<form id="d" action="" method="get">
		<input name="user" type="text" /><br/>
		<input name="pass" type="text" /><br/>
		<select name="color" >
			<option value="red">red</option>
			<option value="blue">blue</option>
		</select><br/>
		<input type="button" value="第一个" onclick="alert(document.getElementById('d').elements[0].value);" />
		<input type="button" value="第2个" onclick="alert(document.getElementById('d').elements['pass'].value);" />
		<input type="button" value="第3个" onclick="alert(document.getElementById('d').color.value);" />
	</form>
</body >

结果
在这里插入图片描述

5- 访问列表框、下拉菜单的选项

属性1介绍
form返回列表框、下拉菜单所在的表单对象。只读属性
length返回列表框、下拉菜单的选项个数。该属性的值可通过增加或刷险选项来改变。只读属性。
options返回列表框、下拉菜单里所有选项组成的数组。只读属性。
selectedIndex返回下拉列表中选中选项的索引,如果有多个选项被选中,第一个被选中选项的索引。读写属性。
type返回下拉列表的类型,即是否允许多选。如果允许多选,则返回sle 如果不支持多选,则返回select-one。
select.options[index]返回列表框、下 拉菜单的第index+1个选项。
form返回包含该选项所处列表框、下拉菜单的表单对象。
defaultSelected返回该选项默认是否被选中。只读属性。
value返回每个选项的value值。
	<select name="co" id="co" size="6">
		<option value="red">red</option>
		<option value="blue">blue</option>
		<option value="python">python</option>
		<option value="php">php</option>
		<option value="c">c</option>
		<option value="a">a</option>
	</select><br/>
	<input type="button" value="第一个" onclick="change(curTarget.options[0]);" />
	<input type="button" value="上一个" onclick="change(curTarget.options[curTarget.selectedIndex -1]);" />
	<input type="button" value="下一个" onclick="change(curTarget.options[curTarget.selectedIndex +1]);" />
	<input type="button" value="最后" onclick="change(curTarget.options[curTarget.length -1]);" />
	<script>
		var curTarget = document.getElementById("co");
		var change = function (target){
			alert(target.text);
		}
	</script>

结果
在这里插入图片描述

6- 访问表格子元素

属性1介绍
caption返回该表格的标题对象。可通过修改该属性来改变表格标题。
HTMLollction roOWs返回该表格里的所有表格行,该属性会返回<thead…/>、<tfoot…/>、<tbody…/>元素里的所有表格行。只读属性
HTMLCollction tBodies返回该表格里所<tbody…/>元素组成的数组
table.rows[index]返回该表格第index+ 1行的表格行
cells返回该表格行内所有的单元格组成的数组。只读属性
rowIndex返回该表格行在表格内的索引值。只读属性。
cellIndex返回该单元格在该表格内的索引值。只读属性
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值