开发者

return DOM in javascript function

I am trying to write a javascript function to parse a bit of the DOM tree and return a part of it to further parse. Despite stepping through the function which seems to be working fine,开发者_如何学JAVA the return of the function shows as undefined to the calling statement. Is there a way to fix this?

from = entityfromid($(value)[0].getElementsByTagName("O1")[0].childNodes[0].childNodes[0].nodeValue).getElementsByTagName("Name")[0].childNodes[0].nodeValue;

function entityfromid(id) {
$($(xmlDoc)[0].getElementsByTagName("Entities")[0].childNodes).each(function (index, value) {
    if(value.getElementsByTagName('Id')[0].childNodes[0].nodeValue == id) {
        return value;
    }
});
}


You need to take the return statement out of the .each(), and instead return from your entityfromid function.

function entityfromid(id) {

    var ret_value;

    $($(xmlDoc)[0].getElementsByTagName("Entities")[0].childNodes).each(function (index, value) {
        if(value.getElementsByTagName('Id')[0].childNodes[0].nodeValue == id) {
            ret_value = value;
            return false;
        }
    });
    return ret_value;
}

Here, when your result is found, it will set the value of the ret_value variable, and do a return false, which breaks the loop.

Then the ret_value is used for the return from your function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜