dsvyc66464 2013-04-15 12:37
浏览 30
已采纳

将jquery函数从表单提交转换为php数组结果

I'm attempting to take the ImageResolver plugin and adapt it to work with a php array.

Stripping the code to this returns the image without a form:

$(function(){

    var url = $('#url').val();
    ImageResolver.resolve(url, function(image){
        if (image) {
            $('#result').html('<img src="' + image + '" alt="">');
        } else {
            $('#result').html('<h2>No image found</h2>');
        }

});
});

I want to adapt it to work within a php foreach loop. results would be replaced on the next class='result' div. IE: after the page has loaded the urls from the query, the function will parse the url and return image link if one is found. I'm guessing I need to use (each) or this(), but I can't figure it out.

can someone point me in the right direction?

  • 写回答

1条回答 默认 最新

  • duandanbeng1829 2013-04-15 16:12
    关注
            <script src="ImageResolver/URI.min.js"></script>
            <script src="ImageResolver/ImageResolver.js"></script>
    
    
    
        <?
            $javascriptarray = 'var urls = [';
            $counter=0;
            foreach (array('http://www.apple.com/','http://github.com/','http://www.test.com/') as $url) 
            {
                if ($counter++ > 0) $javascriptarray .= ',';
                $javascriptarray .= '"'.$url.'"';
            }
            $javascriptarray .= '];';   
        ?>
    <script>    
    <?=$javascriptarray?>
    
    //The ImageResolver will try all the resolvers one after the other
    //in the order of their registration
    
    //Resolvers that guess the image URL
    ImageResolver.register(new FileExtensionResolver());
    ImageResolver.register(new ImgurPageResolver());
    ImageResolver.register(new NineGagResolver());
    ImageResolver.register(new InstagramResolver());
    
    //Resolvers that need extra ajax requests
    ImageResolver.register(new ImgurAlbumResolver());
    ImageResolver.register(new OpengraphResolver());
    ImageResolver.register(new WebpageResolver());
    
    //Some jQuery code to make the demo work
    //Use a crossdomain proxy (required by some plugins)
    $.ajaxPrefilter('text', function(options) {
        options.url = "http://furious-stream-4406.herokuapp.com?src=" + encodeURIComponent(options.url);
    });
    
    $(function(){
    
    
    var length = urls.length,
    url = null;
        for (var i = 0; i < length; i++) {
                url = urls[i];
                ImageResolver.resolve(url, function(image){
                if (image) {
                    $('#result').append('<img src="' + image + '" alt=""><br>');
                } else {
                    $('#result').append('<h2>No image</h2>');
                    //$('#result').append('<h2>No image found for ' + url + '</h2>');
                }
            }); 
        }
    }); 
    
    </script>   
    

    Watch out cause ImageResolver.resolve() works asynchrone you can get unexpected results. Call ImageResolver.resolve() again before the previous call has finished will change url in $('#result').append('<h2>No image found for ' + url + '</h2>'); to the url of your last call by example. To prevent this you need to initialize a new Resolver in the for-loop. see: Javascript prototypes and instance creation

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部