2015-12-13 155 views
0

我在学习如何使用while loop,并提出了一个场景,其中while loop可能适用。JavaScript - while循环

假设20张左右的图片具有类似的URL被布置在一个页面象下面这样: http://img7.italian.uk/pastaimages/6253.jpg
http://img7.italian.uk/pastaimages/4218.jpg
http://img7.italian.uk/pastaimages/7657.jpg
...(根据该模式,仅URL的最后一部分是对于每个不同的图片)...

在所有以http://img7.italian.uk/pastaimages/*开头的图片中,如果第一张图片是http://img7.italian.uk/pastaimages/6253.jpg,只刷新当前页面。

只要第一张照片是“6253”,同样的动作就会重复出现。

但是,一旦第一张图片不再是“6253”之一,则停止循环,并单击具有http://img7.italian.uk/pastaimages/*网址,如“3585”或8291等目前的第一张图片

我不知道怎么样去做这件事。

while (// 1st image === 6253) { 
    //refresh the current page 
} 
// once 1st image is no longer 6253, stop the loop and 
// click the current first picture that ends with something like 3585, 8291 etc. 
+0

为什么不在循环中使用if?似乎只有当第一个图像具有特定ID时,才想继续迭代。 –

回答

0

首先得到图像的SRC(我需要看到你的HTML,但它是这样的):

imgSrc = String(document.getElementById(idOfYourImage).src); 

并把它作为一个条件,while循环:

while (imgSrc == 'http://img7.italian.uk/pastaimages/6253.jpg') { 
    document.location.reload(); //refresh the current page 
} 
// loop stops automatically 

不确定你的意思是点击当前图片。

查看reloadingwhile循环上的文档。

+0

在页面中,最新的图片(一旦可用)出现在页面的左上角(即第一个位置)。因此,以前最新的图片“6253”现在已经移到第二位(或第三等...一旦另一张最新的图片已被添加到页面)。如何在第一个位置点击当前最新的图片,其中包含“3534”,“9043”或其他任何数字?所有图片都有相同的起始网址,只有最后一部分不同。谢谢。 – Segwayinto

+0

点击?你的意思是突出或扩大?你有没有尝试过使用图片插页? – byrass

+0

哦,图像是一个超链接,点击后会导致另一个页面。我想要左键单击图像以转到下一页。我想这是我应该在这批代码的末尾编写的命令,但不知道如何编写它。 – Segwayinto