简体   繁体   English

javascript将元素从一个函数传递到另一个函数

[英]javascript pass element from one function to another

this is a partial code: 这是一个部分代码:

function readXmlUsingXPath(i,guid, xpath) {
    var xmlDoc = loadXML("gameFeed.aspx?guid=" + guid);
    if (xmlDoc == null) { document.getElementById(guid).innerHTML = "UPDATING"; return }
    else { };
    i.innerHTML = xmlDoc.selectNodes(xpath)[0].text;
}

window.onload = function () {
    for (var i = 0; i < document.getElementsByTagName('jackpot').length; i++) {
        var guid = document.getElementsByTagName('jackpot')[i].getAttribute('data-g').split('|')[0];
        var xpath = document.getElementsByTagName('jackpot')[i].getAttribute('data-g').split('|')[1];
        readXmlUsingXPath(document.getElementsByTagName('jackpot')[i],guid, xpath);
    }
}

basicly, what i want to do is pass the element from the onload function, to the readXmlUsingXpath function, so i could change it. 基本上,我想做的是将元素从onload函数传递到readXmlUsingXpath函数,以便我可以对其进行更改。 but i get an unknown runtime error... 但我收到一个未知的运行时错误...

as asked, some sample html: 根据要求,一些示例html:

<li>
<a href="/gamepath/"><span>gameTitle</span>
<span><jackpot data-g="<%=game.Guid %>|<%=game.GamingProperties.JackpotFeedXpath %>">UPDATING . . .</jackpot></span>
</a>
</li>

Well, first of all, yes - cache the results of getElementsByTagName() and .getAttribute('data-g').split('|') into a variable: 好吧,首先,是的-将getElementsByTagName().getAttribute('data-g').split('|')的结果缓存到变量中:

var myDataG = MyJackpotElement.getAttribute('data-g').split('|');

Then instead of processing XPath that comes from your HTML source code, since you pass the guid to a server side script anyway, make the server script process the item (with XPath) on the server side - so it would get the xpath expression from the item you're loading by Guid :) 然后,由于无论如何都将guid传递给服务器端脚本,因此不必处理HTML源代码提供的XPath,而是让服务器脚本在服务器端处理该项目(使用XPath)-这样它将从XPath表达式中获取xpath表达式。您正在通过Guid加载的项目:)

Make the server side script return you the value you're looking for ( gameFeed.aspx?guid= ), not the XML that you will further process on the client side to find the value. 使服务器端脚本返回您正在寻找的值gameFeed.aspx?guid= ),而不是将在客户端进一步处理以找到该值的XML。

Does the code work after refactoring it as per Denis's suggestions? 根据Denis的建议,代码重构后是否可以工作?

One big problem with the way you kept performing a getElementsByTagName, even aside from the huge performance hit, is that if the collection changes for any reason, the length might not be correct anymore, and you could be requesting element 9 where only 8 exist. 除了性能受到巨大影响外,您继续执行getElementsByTagName的方式的一个大问题是,如果集合由于任何原因发生更改,长度可能就不再正确,并且您可能会要求元素9(其中只有8个存在)。

Also, it's possible that changing the innerHTML of an element will throw the browser off. 另外,更改元素的innerHTML可能会使浏览器退出。

Side nit: I've renamed the i parameter of the readXmlUsingXPath function to jackpot , which seems a little more appropriate than just i . readXmlUsingXPath :我已将readXmlUsingXPath函数的i参数重命名为jackpot ,这似乎比仅i更合适。

function readXmlUsingXPath(jackpot, guid, xpath) {
    var xmlDoc = loadXML("gameFeed.aspx?guid=" + guid);
    if (xmlDoc == null) {
        document.getElementById(guid).innerHTML = "UPDATING"; 
        return;
    }
    var firstNode = xmlDoc.selectNodes(xpath).item(0);
    jackpot.innerHTML = firstNode.text;
}

window.onload = function () {
    var jackpots = document.getElementsByTagName('jackpot');
    for (var i = 0; i < jackpots.length; i++) {
        var data = jackpots[i].getAttribute('data-g').split('|'),
        guid = data[0],
        xpath = data[1];
        readXmlUsingXPath(jackpots[i], guid, xpath);
    }
}

ok, so i modified the code a bit, and made it simpler. 好的,所以我对代码进行了一些修改,并使代码更简单。

now it works as its supposed to. 现在它按预期工作了。

function loadXMLDoc(guid,xpath,i) {

    var xmlhttp;
    if (window.XMLHttpRequest) {xmlhttp = new XMLHttpRequest();}
    else {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
    xmlhttp.async = true;
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            i.innerHTML = xmlhttp.responseXML.selectNodes(xpath)[0].text;
        }
    }
    xmlhttp.open("GET", "gameFeed.aspx?guid=" + guid, true);
    xmlhttp.send();
}

var tags = document.getElementsByTagName("pre");
for (var i = 0; i < tags.length; i++) {
    var data = tags[i].getAttribute('data-g').split('|'), guid = data[0], xpath = data[1];
    loadXMLDoc(guid,xpath,tags[i]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将Javascript数据从一个函数传递到另一个函数 - Pass Javascript data from one function to another javascript - 将数据从一个函数传递到另一个函数 - javascript - pass data from one function to another 如何将 javascript 事件从一个元素传递到另一个元素? - How do I pass javascript events from one element to another? 如何在javascript中将变量从一个函数传递给另一个函数 - how to pass variable from one function to another function in javascript 如何在JavaScript中将局部变量从一个函数传递给另一个函数 - how to pass local variable from one function to another function in javascript 如何在原版 JavaScript 中将其从一个 function 传递到另一个 function? - How to pass this from one function to another function in vanilla JavaScript? 如何将参数从一个 function 传递到另一个 function onclick in javascript - How to pass a argument from one function to the another function onclick in javascript 将OnClick事件中的参数从一个Javascript函数传递给另一个Javascript函数 - Pass Parameter From OnClick Event from one Javascript Function to Another 将变量从一个JavaScript回调匿名函数传递给另一个 - Pass variable from one JavaScript callback anonymous function to another 如何将变量从一个 function 传递到另一个 Javascript? - How to pass variable from one function to another Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM