博主信息
博文 11
粉丝 0
评论 0
访问量 23435
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
操作html元素
玄夜的博客
原创
1369人浏览过
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<img src="../img/pic1.gif"  alt="" width="500px"/>
		<h3 id="header" class="hello" style="color: red;" title="当幸福来敲门" index="php.cn">
			《当幸福来敲门》:别走,幸福终究会到来
		</h3>
	</body>
	
	<script>
		//1.原生属性:看做当前元素对象的属性,进行读写
		var img = document.getElementsByTagName('img').item(0);
		console.log(img.src);    //读取原生属性
		img.src="../img/pic2.jpg";   //更新属性
		img.width=800;
		
		//class属性,因为class与js中的关键字冲突,用className来表示
		var h3 = document.getElementById('header');
		console.log(h3.className);
		
		//style属性   它的值不是一个字符串   是一个对象
		h3.style.color="green";
		
		//2.自定义属性
		if(h3.hasAttribute('index')){    //检查属性是否存在
			console.log(h3.getAttribute('index'))    //查看属性
			h3.setAttribute('index','当幸福来敲门')     //更新属性
			console.log(h3.getAttribute('index'))
			h3.removeAttribute('style')              //移除属性
		}
		
		//以上方法有可以用在原生属性
		h3.setAttribute('style','color: yellow;font-size: 12px;');
		
		//attributes以数组的方式来获取元素的属性   这时属性是只读的   获取的是实时更新的
		console.log(h3.attributes);
		console.log(h3.attributes[0]);
		console.log(h3.attributes['id']);
		console.log(h3.attributes['style']);
	</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+教程免费学