源代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>标签的属性</title>
</head>
<body>
<img src="images/1.jpg" alt="我是alt222" title="我是title" nj="666">
<script type="text/javascript">
//获取
let oImg=document.querySelector("img");
console.log(oImg.alt);
console.log(oImg.getAttribute("nj"));
// 2.修改
/*let oImg1=document.querySelector("img");
oImg1.title="新的title";
oImg1.setAttribute("nj","123"); */
// 3.增加
/*let oImg2=document.querySelector("img");
oImg2.setAttribute("it666","itzb");*/
// 4.删除
/*let oImg3=document.querySelector("img");
oImg3.alt="";
oImg3.removeAttribute("nj");*/
</script>
</body>
</html>
对象.属性是对系统自带的属性进行操作. Attribute是对自己写的属性进行操作。(自定义)