<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> * { margin: 0; padding: 0; } </style> </head> <body> <a href="http://www.baidu.com" id="box" class="box" target="_black" tz="xp">去百度</a> <script> /* js操作元素的标签属性: 规范的标签属性: . 符号直接操作(可读可写) 不规范(自定义)的标签属性: 获取:.getAttribute() 设置:.setAttribute() 移除:.removeAttribute() 注意: 所有的 路径、颜色 获取的结果不一定是你写的内容 通过id获取的元素赋值给变量后,假设修改了id,这个变量还是表示这个元素 自定义标签属性的操作方式,同样可以操作符合规范的标签属性 */ var oA = document.getElementById("box"); // alert(oA.target);//可读 // oA.target = "_self";//可写 // alert(oA.className);//class属性 要用className // oA.className = ""; // alert(oA.getAttribute("tz")); // oA.setAttribute("tz","xxp"); // oA.setAttribute("py","web"); // oA.removeAttribute("class"); // alert(oA.href) oA.id = "wrap"; oA.target = "_self"; </script> </body> </html>