Change image-link after button click

时间秒杀一切 提交于 2020-01-06 21:59:30

问题


I'm using Bing image search API in my blog. The response of my request:

stdClass Object

    (
        [d] => stdClass Object
            (
                [results] => Array
                    (
                        [0] => stdClass Object
                            (
                                [__metadata] => stdClass Object
                                    (
                                        [uri] => https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query='Kitchen'&Market='en-us'&$skip=0&$top=1
                                        [type] => ImageResult
                                    )

                                [ID] => a40b8c85-8a6b-45a8-bce2-c07b16a942e6
                                [Title] => Our Kitchen Remodel is Complete!!! @ A Well Dressed Home
                                [MediaUrl] => http://awelldressedhome.com/wp-content/uploads/2010/10/Kitchen-31.jpg
                                [SourceUrl] => http://awelldressedhome.com/496-our-kitchen-remodel-is-complete/
                                [DisplayUrl] => awelldressedhome.com/496-our-kitchen-remodel-is-complete
                                [Width] => 4000
                                [Height] => 3000
                                [FileSize] => 5062458
                                [ContentType] => image/jpeg

And I get 50 results(50 different images and URLs) in this response. I have a php code which give me the link of first image in my response:

  <?php
            $context = stream_context_create($data);
            $response = file_get_contents($requestUri, 0, $context);
            $response=json_decode($response);
            $response = $response->d->results[0]->MediaUrl;
            echo "<pre>";
            print_r($response);
            echo "</pre>";
            echo '
                <td>
                 <img src="'.$response.'" height="100" weight="100">
                </td>
            ';
            echo("</ul>");
        ?>

Also I made a simple button, which gives me this first image which I got earlier from my response:

<div class="form-group">
    <button onclick="myFunction()">Click me</button>
    <p id="demo"></p>
    <script>
        function myFunction()
        {
             document.getElementById("demo").innerHTML ="<?php echo "<img src=".$response." height='100' width='100'/>"?>"
        }
    </script>
</div>

So the question is: How can I get new image each time when I click my button? Sorry for my English and thanks for help.


回答1:


You will first need to make sure to write your url's into javascript:

<?php
    $urlList = array();
    foreach($response->d->results as $r){
        $urlList[] = $r->MediaUrl;
    }
?>
<script>
    var urls = <?php echo json_encode($urlList); ?>;
</script>

and then you can use those url's in your function:

<div class="form-group">
    <button onclick="myFunction()">Click me</button>
    <p id="demo"></p>
    <script>
        var currentIndex = 0;
        function myFunction(){
             document.getElementById("demo").innerHTML = "<img src='" + urls[currentIndex++ % urls.length]  + "' height='100' width='100'/>";
        }
    </script>
</div>


来源:https://stackoverflow.com/questions/34159082/change-image-link-after-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!