html代码:(news.html)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//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=UTF-8"/>
<title>新闻数据管理</title>
</head>
<body> <SCRIPT LANGUAGE="JavaScript">
<!--
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
xmlDoc.async = false;
xmlDoc.load("news.xml");
//-->
</SCRIPT>
<!-- 新闻数据显示 -->
<xml id="n" src="news.xml">
</xml>
<table DATASRC=#n border="1">
<caption><h2>新闻管理</h2></caption>
<thead>
<th>新闻ID</th>
<th>标题</th>
<th>内容</th>
<th>发布日期</th>
<th><input type="button" value="新增" /></th>
</thead>
<tr>
<td><span DATAFLD="id"></span></td>
<td><span DATAFLD="title"></span></td>
<td><span DATAFLD="content"></span></td>
<td><span DATAFLD="pdate"></span></td>
<td>
<input type="button" value="修改" />
<input type="button" value="删除" />
</td>
</tr>
</table>
</body>
</html>
-------------------------------------------------------
xml文件:(news.xml)
<?xml version="1.0" encoding="gb2312"?>
<root>
<news>
<id>4</id>
<title>新闻4</title>
<content>新闻444444444444444444444</content>
<pdate>2009-9-21</pdate>
</news>
<news>
<id>3</id>
<title>新闻3</title>
<content>新闻333333333333333333333</content>
<pdate>2009-9-21</pdate>
</news>
<news>
<id>2</id>
<title>新闻2</title>
<content>新闻22222222222222222</content>
<pdate>2009-9-21</pdate>
</news>
<news>
<id>1</id>
<title>新闻1</title>
<content>新闻11111111111111111111111111</content>
<pdate>2009-9-21</pdate>
</news>
</root>
--------------------------------------------------
我想用JavaScript操作XML文件来管理新闻数据
用XML数据岛显示出数据后,怎样才能得到每条数据的ID?
也就是点击每条数据后的修改或删除按钮后怎样才能知道我要修改的是哪条数据?我在id的<span>标签里加了id="newsid",然后用document.getElementById("newsid").innerText;方法来得到id,不过每次就只能拿到最后一条数据的ID。请高手帮忙看看!谢了!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//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=UTF-8"/>
<title>新闻数据管理</title>
</head>
<body> <SCRIPT LANGUAGE="JavaScript">
<!--
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
xmlDoc.async = false;
xmlDoc.load("news.xml");
//-->
</SCRIPT>
<!-- 新闻数据显示 -->
<xml id="n" src="news.xml">
</xml>
<table DATASRC=#n border="1">
<caption><h2>新闻管理</h2></caption>
<thead>
<th>新闻ID</th>
<th>标题</th>
<th>内容</th>
<th>发布日期</th>
<th><input type="button" value="新增" /></th>
</thead>
<tr>
<td><span DATAFLD="id"></span></td>
<td><span DATAFLD="title"></span></td>
<td><span DATAFLD="content"></span></td>
<td><span DATAFLD="pdate"></span></td>
<td>
<input type="button" value="修改" />
<input type="button" value="删除" />
</td>
</tr>
</table>
</body>
</html>
-------------------------------------------------------
xml文件:(news.xml)
<?xml version="1.0" encoding="gb2312"?>
<root>
<news>
<id>4</id>
<title>新闻4</title>
<content>新闻444444444444444444444</content>
<pdate>2009-9-21</pdate>
</news>
<news>
<id>3</id>
<title>新闻3</title>
<content>新闻333333333333333333333</content>
<pdate>2009-9-21</pdate>
</news>
<news>
<id>2</id>
<title>新闻2</title>
<content>新闻22222222222222222</content>
<pdate>2009-9-21</pdate>
</news>
<news>
<id>1</id>
<title>新闻1</title>
<content>新闻11111111111111111111111111</content>
<pdate>2009-9-21</pdate>
</news>
</root>
--------------------------------------------------
我想用JavaScript操作XML文件来管理新闻数据
用XML数据岛显示出数据后,怎样才能得到每条数据的ID?
也就是点击每条数据后的修改或删除按钮后怎样才能知道我要修改的是哪条数据?我在id的<span>标签里加了id="newsid",然后用document.getElementById("newsid").innerText;方法来得到id,不过每次就只能拿到最后一条数据的ID。请高手帮忙看看!谢了!
<script>
var i = 0;
document.writer("<span id='newsid'+i></span>");
<script>
数据显示是用XML数据岛,在html页面的table的td中用<span DATAFLD="id"> </span>就能显示数据,
id是这样加的<span id="newsid" DATAFLD="id"> </span>,然后用document.getElementById("newsid").innerText;取得table的td中的文本,就得到了id,不过这样只能拿到最后一条数据的id。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//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=UTF-8"/>
<title>新闻数据管理 </title>
</head>
<body> <SCRIPT LANGUAGE="JavaScript">
<!--
window.onload = function(){
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
xmlDoc.async = false;
xmlDoc.load("news.xml");
var dele = document.getElementsByTagName('input');
for(var i = 0; i < dele.length; i++){(function(i){
if(dele[i].id == 'dele'){
dele[i].onclick = function(e){
e = e || getEvent(e);
var elem = getTarget(e);
e.cancelBubble = true;
var row = elem.parentElement.parentElement;
alert(row.rowIndex);
};
}
})(i)}
var getEvent = function(e){
return e || window.event;
};
var getTarget = function(e){
return e.target || e.srcElement;
};
};
//--> </SCRIPT> <!-- 新闻数据显示 -->
<xml id="n" src="news.xml">
</xml>
<table id="myTable" DATASRC="#n" border="1">
<tbody>
<caption> <h2>新闻管理 </h2> </caption>
<thead>
<th>新闻ID </th>
<th>标题 </th>
<th>内容 </th>
<th>发布日期 </th>
<th> <input type="button" value="新增" /> </th>
</thead>
<tr>
<td> <span DATAFLD="id"> </span> </td>
<td> <span DATAFLD="title"> </span> </td>
<td> <span DATAFLD="content"> </span> </td>
<td> <span DATAFLD="pdate"> </span> </td>
<td>
<input type="button" value="修改" />
<input type="button" id="dele" value="删除" />
</td>
</tr>
</tbody>
</table>
</body>
</html>