getAttribute与setAttribute用法
2014-12-30 16:15
260 查看
getAttribute和setAttribute只能用于元素节点。
1.当用getElementById获得元素节点时
/*---------------------------index.html---------------------------*/
<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Author" content="Hehe">
<title>Shopping list</title>
</head>
<body>
<p id="purchases" title="one">What to buy</p>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
/*------------------------script.js---------------------------*/
var paras=document.getElementById("purchases");
alert(paras.getAttribute("title"));
paras.setAttribute("title","abcd");
alert(paras.getAttribute("title"));
此时警告框一个显示one,一个显示abcd。
2.当用getElementsByTagName获得元素节点时
/*---------------------index.html-----------------------------*/
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Author" content="Hehe">
<title>Shopping list</title>
</head>
<body>
<p id="purchases" title="one">What to buy</p>
<p title="two">What to buy</p>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
/*----------------------------script.js--------------------*/
var paras=document.getElementsByTagName("p");
for(var i=0;i<paras.length;i++){
paras[i].setAttribute("title","abcd");
alert(paras[i].getAttribute("title"));
}
此时有俩个警告框显示abcd。
注:getElementById返回的是节点,getElementsByTagName返回的是数组。
getAttribute("")与setAttribute("",A)都需要用到双引号A="字符"(即1.setAttribute("","a list of and so on")。2.A=”a list of and so on";setAttribute("",A)。)。
alert()不是显示字符串不需要用到双引号。
1.当用getElementById获得元素节点时
/*---------------------------index.html---------------------------*/
<!DOCTYPE html>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Author" content="Hehe">
<title>Shopping list</title>
</head>
<body>
<p id="purchases" title="one">What to buy</p>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
/*------------------------script.js---------------------------*/
var paras=document.getElementById("purchases");
alert(paras.getAttribute("title"));
paras.setAttribute("title","abcd");
alert(paras.getAttribute("title"));
此时警告框一个显示one,一个显示abcd。
2.当用getElementsByTagName获得元素节点时
/*---------------------index.html-----------------------------*/
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Author" content="Hehe">
<title>Shopping list</title>
</head>
<body>
<p id="purchases" title="one">What to buy</p>
<p title="two">What to buy</p>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
/*----------------------------script.js--------------------*/
var paras=document.getElementsByTagName("p");
for(var i=0;i<paras.length;i++){
paras[i].setAttribute("title","abcd");
alert(paras[i].getAttribute("title"));
}
此时有俩个警告框显示abcd。
注:getElementById返回的是节点,getElementsByTagName返回的是数组。
getAttribute("")与setAttribute("",A)都需要用到双引号A="字符"(即1.setAttribute("","a list of and so on")。2.A=”a list of and so on";setAttribute("",A)。)。
alert()不是显示字符串不需要用到双引号。
相关文章推荐
- js-getAttribute与setAttribute的用法
- 设置 取消input的disabled属性 getattribute(),setAttribute(),removeattribute()用法
- jsp中setAttribute,getAttribute和getParameter用法
- js中setAttribute()和getAttribute()用法
- getAttribute 与 setAttribute 用法
- 学习笔记---getAttribute与setAttribute 的用法
- 关于JSP中getAttribute()和setAttribute()的用法
- setAttribute、getAttribute和getParameter
- DOM方法(getElementById, getElementsByTagName, getAttribute, setAttribute)
- JavaScript中setAttribute用法
- dom(getElementsByName()\getElementsByName()\getAttribute()\setAttribute()\节点属性)
- QT中的setAttribute()用法
- JavaScript中setAttribute用法详解
- HttpServletRequest类既有getAttribute()方法,也有getParameter()方法,有setAttribute()方法,而没有setParameter()方法
- getAttribute()和setAttribute() 的小练习
- 关于jsp与servlet之间传值的问题及getParameter和getAttribute的用法
- request的setAttribute()用法及request.sendRedirect 与 request.getRequestDispatcher.forward 的区别
- InvocationTargetException异常的深入研究-servlet的setAttribute与getAttribute
- JavaScript方法之getAttribute()和setAttribute()方法
- getAttribute、setAttribute和removeAttribute