博主信息
博文 11
粉丝 0
评论 0
访问量 23426
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
根据name和标签名获取元素
玄夜的博客
原创
1507人浏览过
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<img src="../img/pic1.gif" alt="" name="pic">
		<img src="../img/pic2.jpg" alt="" name="pic1">
		<form action="" name="register">
			<input type="text" placeholder="用户名" />
			<input type="text" placeholder="密码不少于8位" />
			<button>提交</button>
		</form>
		<p><a href="http://www.php.cn" name="php">php中文网</a></p>
	</body>
	
	<script>
		//以文档对象的方式来访问这些特定的元素集合
		//document.images   获取所有的<img>元素,返回是一个数组
		document.images[0].style.width = '500px';   //标签的数字索引
		document.images['pic1'].style.width = '800px';   //name属性
		//如果将images看成对象,name就可以看成属性
		document.images.pic.style.width = '250px';   //name作为images对象的元素属性
		
		//forms属性   获取页面中所有<form>
		document.forms[0].style.background = 'pink';
		document.forms['register'].style.background = 'yellow'
		document.forms.register.style.background = 'red'
		document.forms.item(0).style.background = 'gray'   //元素的集合item方法
		
		//links属性   获取页面中所有<a>
		document.links[0].style.background = 'pink';
		document.links['php'].style.background = 'yellow';
		document.links.php.style.background = 'red';
		document.links.item(0).style.background = 'gray';
		
		//body属性   获取页面中<body>
		document.body.style.background = 'yellow';
		
		//head属性   获取页面中<head>
		var style = document.createElement('style');
		document.head.appendChild(style);
		
		//documentElement  获取页面中<html>
		console.log(document.documentElement);
		
		//doctype   获取文档类型
		console.log(document.doctype);
	</script>
</html>


本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学