代码之家  ›  专栏  ›  技术社区  ›  certainlyakey

用变量替换静态ID时出现问题

  •  0
  • certainlyakey  · 技术社区  · 17 年前

    我有一个脚本,它从外部XML文件加载信息,并在单击时将其插入HTML页面。下面是这个脚本的代码:

    var xmlhttp;
    function loadXMLDoc(url,target) {
    xmlhttp = null;
    if (window.XMLHttpRequest) { // code for all new browsers
        xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) { // code for IE5 and IE6
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (xmlhttp != null) {
        xmlhttp.onreadystatechange = state_Change;
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    }
    else {
        alert("Your browser does not support XMLHTTP.");
    }
    }
    function state_Change() {
    if (xmlhttp.readyState == 4) { // 4 = "loaded"
        if (xmlhttp.status == 200) { // 200 = OK    
            var markers = xmlhttp.responseXML.documentElement.getElementsByTagName("marker");
            for (var i = 0; i < markers.length; i++) {
                var name = markers[i].getElementsByTagName("name")[0].firstChild.nodeValue;
                //etc...
    document.getElementById(target).innerHTML += '<li>' + name + '</li>\n';
            }
        }
        else {
            alert("Problem retrieving XML data");
        }
    }
    }
    

    这是HTML:

      <ul id="list_puncts">
    <li><a href="javascript:;" onclick="loadXMLDoc('./content/geo_points/slovenia.xml','list_sl')">Republika Slovenija (RS)</a>
     <ul id="list_sl">
       <!--here should be some info from XML file-->
         </ul></li>
         <li><a href="javascript:;" onclick="loadXMLDoc('./content/geo_points/horvatia.xml','list_hr')">Republika Hrvatska (RH)</a>
        <ul id="list_hr">
        <!--here should be some info from XML file-->
        </ul></li>
       </ul>
    

    但是,它不起作用-单击链接后,XML被加载(可以在Firebug中看到),但第二个变量- target -无法进入 state_Change 函数,因此不执行实际操作。如果 目标 在里面 document.getElementById(target).innerHTML 被某个静态ID(如 list_sl ,它是有效的,但是我在HTML中有很多这样的链接,不仅是斯洛文尼亚和Horvatia,所以非常需要这个变量。

    谢谢你的帮助。

    2 回复  |  直到 12 年前
        1
  •  1
  •   Nate    17 年前

    state_Change 匿名函数中的逻辑 loadXMLDoc .

    function loadXMLDoc(url,target) {
        // ... code to instantiate XMLHttprequest goes here ...
    
        if (xmlhttp != null) {
            xmlhttp.onreadystatechange = function() {
                // Your state_Change() logic goes here.
                // This has access to the variable "target" because
                // it's within the same enclosing function's scope.
            };
            xmlhttp.open("GET", url, true);
            xmlhttp.send(null);
        }
    }
    

    这样做的缺点是,每次都会实例化一个新的函数实例。 Load XMLDOC 被称为。

        2
  •  1
  •   Josef Pfleger    17 年前

    如果使用全局变量(两者都用于 XMLHttpRequest S和你的 target s),您不能支持并行请求(即用户在一个链接之前单击另一个链接) 对象 返回)。实现它的一种方法是 xmlhttp 你的变量 loadXMLDoc 函数的作用域并添加 目标 作为财产 对象 对象:

    function loadXMLDoc(url, target) {
        var xmlhttp;
        // create XMLHttpRequest instance
        if(xmlhttp != null) {
            xmlhttp.onreadystatechange = state_Change;
            xmlhttp.targetListId = target;
            xmlhttp.open(); // etc.
        }
    };
    

    在你 state_Change 您可以访问的函数 对象 使用事件的实例 目标 财产:

    function state_Change(e) {
        var xmlhttp = e.target;
        // check readystate & status
    
        var targetList = document.getElementById(xmlhttp.targetListId);
        targetList.innerHTML = ''; // fill with data, etc.
    };