簡體   English   中英

單擊按鈕后更改圖像鏈接

[英]Change image-link after button click

我在博客中使用Bing圖片搜索API。 我的請求的回復:

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

在此響應中,我得到了50個結果(50個不同的圖像和URL)。 我有一個php代碼,可在響應中給我第一張圖片的鏈接:

  <?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>");
        ?>

我還做了一個簡單的按鈕,它給了我第一張我從響應中得到的第一張圖片:

<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>

所以問題是:每次單擊按鈕時如何獲得新圖像? 對不起,我的英語,謝謝您的幫助。

您首先需要確保將您的網址寫入javascript:

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

然后您可以在函數中使用這些網址:

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM