duanjiao7440 2013-05-21 15:19
浏览 22
已采纳

实时更改图像

Well, I've just started a project and I'm already stuck, I real need a help.

I have 16 (4x4) images and I'm presenting them in a page.

$max_images = $_GET['images'];
$num_images = $max_images;
while  (($num_images > 0) && ($num_images <= $max_images))
{
echo '<img class="nomarge" alt="" src="image' . $num_images . '.jpeg"/>';
$num_images--;
}

They all have names like "image1.jpeg, image2.jpeg, ..." so this will work. Now, I want to change only one of the 16 images without a reload, and only if the image in the source is different from the one shown.

Basically, I think I need to build something similar to the code presented in the stock markets (http://economico.sapo.pt/ is an excellent example of what I'm trying to do, on the right down corner, it shows a stock variation that is always changing, and it is not always reloading all the page, not even all the results, only the ones that change)

What is the best way to do that, and do you know any examples like these with images?

  • 写回答

1条回答 默认 最新

  • doule0941 2013-05-21 16:01
    关注

    In order to change one of your images, the best practice is usually to attach an id to each of your <img> tags, that way you can select one specifically by its id.

    Your current code isn't necessarily bad, but the normal convention for iterating through something like that would be to use a for loop, as follows:

    $count = $_GET['images'];
    
    for ($i = 1; $i <= count; $i++){
        echo '<img class="nomarge" alt="" src="image' . $i . '.jpeg" id="image' . $i  .'" />';
    }
    

    Notice I added id to the end of each tag and now your images have the ids image1, image2 ... image16.

    Now, I want to change only one of the 16 images without a reload, and only if the image in the source is different from the one shown.

    I'm not exactly sure what this means, but assuming you have detected that 'the image in the source is different from the one shown', you can select a specific image and replace its source using the following jQuery:

    $("#image1").attr("src", "https://www.newimagesource.com" );
    

    Hope this helps!

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

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失