日期:2014-05-16  浏览次数:20361 次

学习做菜单 2008-4-25
有这样一个 xml:

<?xml version="1.0" encoding="utf-8" ?>
<Menu>
  <MenuNode Url="abc.html" NormalPath="abc.jpg" OverPath="abc_over.jpg" OnTips="产品" />
  <MenuNode Url="def.html" NormalPath="def.jpg" OverPath="def_over.jpg" OnTips="新闻" />
  <MenuNode Url="ghi.html" NormalPath="ghi.jpg" OverPath="ghi_over.jpg" OnTips="反馈" />
</Menu>

如何用 javascript 读取每一个<MenuNode>,并生成图片链接排列在页面上?
Url 表示链接指向的URL
NormalPath 表示正常状态下的图片
OverPath 表示鼠标移动到图片上的状态
OnTips 表示鼠标提示

50 分求助,谢谢。

------解决方案--------------------
你自己把图片换上就可以了...

JScript code
<!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>Read XMLData</title>
<script type="text/javascript">
var loadXML = function (path, callback) {
    if (window.ActiveXObject) {
        var xml = new window.ActiveXObject('Microsoft.XMLDOM');
        xml.async = false;
        xml.load(path);
        callback(xml);
    } else if (document.implementation && document.implementation.createDocument) {
        var xml = document.implementation.createDocument("", "", null); 
        xml.load(path); 
        xml.onload = function () { callback(xml); };
    }
};
window.onload = function () {
    loadXML('xml.xml', function (xml) {
        var datas = xml.getElementsByTagName('MenuNode'), htmls = [], i = 0, t, l;
        for (l = datas.length ; i < l ; i ++) {
            t = datas[i], htmls[htmls.length] = '<a href="' + t.getAttribute('Url') + '"><img src="'
                + t.getAttribute('NormalPath') + '" alt="' + t.getAttribute('OnTips') + '" onmouseover="this.src=\''
                + t.getAttribute('OverPath') + '\'" onmouseout="this.src=\'' + t.getAttribute('NormalPath') + '\'" \/><\/a>';
        }
        document.getElementById('data').innerHTML = htmls.join('<br \/>');
    });
};
</script>
</head>
<body>
<div id="data"></div>
</body>
</html>