我试图在 JavaScript 中使用日期变量来引用 XML 文档中的标记,然后返回Current
在该标记中调用的属性的值。但没有成功。
我确定我只是遗漏了一些东西,但无法弄清楚是什么......非常感谢任何帮助。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script>
function loadXMLDoc(weekscalendar) {
if (window.XMLHttpRequest) {
xhttp=new XMLHttpRequest();
} else {
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET","weeks.xml",false);
xhttp.send();
return xhttp.responseXML;
}
</script>
</head>
<body>
<script>
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
today = dd + mm + yyyy;
xmlDoc=loadXMLDoc("weeks.xml");
x=xmlDoc.getElementsByTagName (today);
var week = x.getAttributeNode("Current").nodeValue; //the nodevalue is the week
document.write(week);
</script>
</body>
</html>
我的 XML 文件的摘录如下:
<?xml version="1.0" encoding="UTF-8"?>
<Portal>
<23062013 Current="Week 13 (23/06/2013 - 29/06/2013)" Last="Week 12 (16/06/2013 - 22/06/2013)" Next="Week 14 (30/06/2013 - 06/07/2013)" />
<24062013 Current="Week 13 (23/06/2013 - 29/06/2013)" Last="Week 12 (16/06/2013 - 22/06/2013)" Next="Week 14 (30/06/2013 - 06/07/2013)" />
</Portal>