开发者

Find a descendent element by ID through frames

Assuming that all the frames are on the same domain, I need to start at a given node (or document), and search for an element with a given ID. If it's not found in the current document, I need to recursively (breadth-first) search all descendent iframes.

Unless I开发者_运维技巧'm missing some gotcha, I'm sure I could write this function, but I'm sure someone's already done it.

I don't use jQuery in my app, so although that's fine to mention for other SO readers, it won't help me. I do, however, use Prototype, and it can be assumed that all descendent iframes will have prototype included.

Any advice?


I think you can get it recursively like this:

function findElement(wdw, id)
{
    var el = wdw.document.getElementById(id);
    if(el) return el;
    for(var i=0; i<wdw.frames.length; i++)
    {
        var el = findElement(wdw.frames[i].window, id);
        if(el) return el;
    }

    return null;
}

and call it like this:

findElement(window, "aa");

here is jsFiddler link: http://jsfiddle.net/QxL7z/2/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜