How do you grab an element from a remote page usin

2019-04-16 06:42发布

I'm using MooTools ( part of the project ) to load a page using Request.HTML which works fine except that I don't want the whole page, just one fragment which has an id.

This is the code in question

var req = new Request.HTML({
    onSuccess: function( res ) {
        // according to the docs
        // res should be the node list of the remote response
        // I want to grab #myFragment

        var f = res.getElementById('myFragment');
        // res.getElementById is not a function

        var f = $(res).getElementById('myFragment');
        // $(res) is null ?

        var f = $$(res).getElementById('myFragment');
        // [null, null] ??


        // more code

    }
}).get('/myurl');

I'm pretty sure this must be possible, I can grab elements that have a class. Does anyone know how to do this.

Thanks )

2条回答
Viruses.
2楼-- · 2019-04-16 07:15

I hopped onto the #mootools channel on irc.freenode.net and got my answer from <kamicane> himself

var req = new Request.HTML({
    onSuccess: function( responseTree, responseElements /*more*/  ) {
        // responseElements is the one I want
        //it's an array of elements which you can filter
        var f = responseElements.filter('#myFragment');

        // do stuff with my fragment

    }
}).get('/myurl');
查看更多
戒情不戒烟
3楼-- · 2019-04-16 07:19

I'm not familiar with Mootools but when digging into Request.HTML documentation found this:

Request success event: onSuccess(responseTree, responseElements, responseHTML, responseJavaScript)

and

responseElements - (array) An array containing all elements of the remote response.

Hope this would give right direction to solve your problem.

查看更多
登录 后发表回答