屬性節點:
某一指定的元素節點的屬性。
代碼如下:
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>JavaScript</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript"> window.onload = function(){ //獲取節點 var NameNode = document.getElementById("name"); //讀取節點屬性值 alert(NameNode.value); //重寫屬性值 NameNode.value ="Mike"; } </script> </head> <body> <p>你喜歡哪個城市</p> <ul id="City"> <li id="bj" name="beijing">北京</li> <li id="Sh">上海</li> <li>廣州</li> <li>深圳</li> </ul> <br/> <p>你喜歡哪款單機遊戲</p> <ul id="game"> <li>反恐精英</li> <li>魔獸</li> <li>紅警</li> <li>實況</li> </ul> <br/> <br/> Name:<input id="name" type="text" name="username" value="LoveCoffee"/> </body> </html>
說明:
操作屬性的一般步驟:
1.獲得節點(根據獲取元素節點的函數獲取)
2.讀取屬性值
3.重寫屬性值