华恩JAVA班第53天

来源:互联网 发布:民族虚无主义知乎 编辑:程序博客网 时间:2024/06/06 00:10

华恩JAVA班第53天

 

访问节点属性的值:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
<title>使用getAttribute读取节点的属性值</title>
<style type="text/css">
img{
 border: 0px;
 float: left;
 padding:3px;
 }
body{
 margin:0px;
 font-size:12px;
 line-height:20px;
}
input{
 margin-top: 5px;
}
</style>
<script type="text/javascript">
function hh(){
 varhText=document.getElementByIdx_x_x("fruit").getAttribute("src");
 
 alert("图片的路径是:"+hText)
 }
function check(){
 varfavor=document.getElementsByName("enjoy");
 var like="你喜欢的水果是:";
 for(vari=0;i<favor.length;i++){
  if(favor[i].checked==true){
   like+="\n"+favor[i].getAttribute("value");
   }
  }
  alert(like);
 }
function change(){
 varimgs=document.getElementsByTagName_r("img");
 imgs[0].setAttribute("src","images/grape.jpg");
 }
</script>
</head>

<body>
<img src="images/fruit.jpg" alt="水果图片" id="fruit" />
<h1id="love">选择你喜欢的水果:</h1>
<input name="enjoy" type="checkbox" value="apple"/>苹果
<input name="enjoy" type="checkbox" value="banana"/>香蕉
<input name="enjoy" type="checkbox" value="grape"/>葡萄
<input name="enjoy" type="checkbox" value="pear"/>梨
<input name="enjoy" type="checkbox"value="watermelon" />西瓜
<br />
<input name="btn" type="button" value="显示图片路径"onclick="hh()" />
<br /><inputname="btn" type="button" value="喜欢的水果" onclick="check()"/>
<br /><inputname="btn" type="button" value="改变图片" onclick="change()"/>
</body>
</html>

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
<title>使用HTMLDOM对象的属性访问节点</title>
<script type="text/javascript">
function change(){
 var imgs=document.getElementByIdx_x_x("s1");
 imgs.src="images/grape.jpg";
 }
 
function show(){
 varhText=document.getElementByIdx_x_x("s1").alt;
 alert("图片的alt是:"+hText)
 }
</script>
</head>

<body>
<img src="images/fruit.jpg" id="s1" alt="水果图片"/><br />
<input name="b1" type="button" value="改变图片"onclick="change()" />
<input name="b2" type="button" value="显示图片路径"onclick="show()" />
</body>
</html>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
<title>使用HTMLDOM操作表格</title>
<style type="text/css">
body{
 font-size:13px;
 line-height:25px;
 }
table{
 border-top: 1px solid #333;
 border-left: 1px solid #333;
 width:300px;
}
td{
 border-right: 1px solid #333;
 border-bottom: 1px solid #333;
 }
.center{
 text-align:center;
}
.title{
 text-align:center;
 font-weight:bold;
 background-color: #cccccc;
}
     
</style>
<script type="text/javascript">
function addRow(){ 
var newRow=document.getElementByIdx_x_x("myTable").insertRow(2);
var col1=newRow.insertCell(0);
col1.innerHTML="幸福从天而降";
var col2=newRow.insertCell(1);
col2.innerHTML="&yen;18.5";
col2.align="center";
 }
 
function updateRow(){
 varuRow=document.getElementByIdx_x_x("myTable").rows[0];
 uRow.className="title";
 }

function delRow(){
   document.getElementByIdx_x_x("myTable").deleteRow(1);
 }
</script>
</head>

<body>
<table border="0" cellspacing="0" cellpadding="0"id="myTable">
  <trid="row1">
   <td>书名</td>
   <td>价格</td>
  </tr>
  <trid="row2">
   <td>看得见风景的房间</td>
   <tdclass="center">&yen;30.00</td>
  </tr>
  <trid="row3">
   <td>60个瞬间</td>
   <tdclass="center">&yen;32.00</td>
  </tr>
</table>
<input name="b1" type="button" value="增加一行"onclick="addRow()" />
<input name="b2" type="button"value="删除第2行" onclick="delRow()"/>
<input name="b3" type="button"value="修改标题" onclick="updateRow()"/>
</body>
</html>

 

 

 

更多信息可以参见同学富晓磊的博客:http://blog.sina.com.cn/u/1798827371

原创粉丝点击