2015-10-18 33 views
1

我不知道如何使用增量。通過一個函數 。我不能讓該段以顯示陣列話 你如何使用增量?

<p id= "demo" 
 
var Array = ["hello", "goodbye"]; 
 
var mimg = document.getElementById(imageArray[0]); 
 
mimg.setAttribute('src', [index]); 
 
//var ArrayIndex = 0; 
 

 
function change() { 
 
\t ("src", Array[Index]); 
 
\t imageIndex++; 
 
\t if (Index >= Array.length) { 
 
\t \t Index = 0; 
 
\t } 
 

 
}

+0

你必須從某個地方定義imageIndex指定的....(外changeImage功能) – sinisake

+0

你會怎麼做呢? imageIndex指定=& – sp000142

+0

如果你想從第一個數組成員開始,它應該是0,對吧? – sinisake

回答

1

不要忘記使用您的瀏覽器的控制檯,read this article Using Your Browser to Diagnose JavaScript Errors

不要使用setattribute函數,請使用src屬性。

var myImage = document.getElementById("mainImage"); 
 

 
var imageArray = ["http://lorempixel.com/400/200/sports/1/", "http://lorempixel.com/400/200/sports/2/", "http://lorempixel.com/400/200/sports/3/", "http://lorempixel.com/400/200/sports/4/"]; 
 

 
myImage.src = imageArray[0]; 
 

 
var imageIndex = 0; 
 

 
function changeImage() { 
 
    myImage.src = imageArray[imageIndex]; 
 
    imageIndex++; 
 
    if (imageIndex >= imageArray.length) 
 
     imageIndex = 0; 
 
} 
 

 
window.onload = function() { 
 
    setInterval(function() { 
 
     changeImage(); 
 
    }, 1000); 
 
};
<img id="mainImage" />

+0

這幫助我瞭解我在做什麼,但圖像不會加載,你把img id放在身體裏嗎? – sp000142

+0

'' – sp000142

+0

@ sp000142你需要調用在onload事件的功能。 http://stackoverflow.com/questions/20180251/when-to-use-window-onload –

0
var myImage = document.getElementById("mainImage"); 
var imageArray = ["images/1.png","images/2.png","images/3.png","images/4.png"]; 
var mimg=document.getElementById(imageArray[0]); 
mimg.setAttribute('src',photos[index]); 

您未能顯示相關的HTML,但我在本節中,與ID「圖像獲取元素通知/ 1.png「,並將該元素的src設置爲photos [index]中的某個值。你沒有展示如何加載照片數組。你實際上是否擁有ID爲「images/1.png」的元素?

在函數中,您將mainImage的src設置爲imageArray中的值,而不是照片數組中的值。這可能是有效的,但由於這與你在函數之外做的不同,我想確保這是有意的。

0

我認爲你是在談論這樣的解決方案:

var imageArr=["images/1.png", "images/2.png", "images/3.png", "images/4.png"]; 

$('#button'). on('click',function(){ 
var index=(Math.random(0,imageArr.length)*10) 
$('#img').attr('src',imageArr[index]) 
}); 

同樣,你的問題是不明確的,因此,我認爲這將有助於你得到的方向。

0

這應該是,如果你使用的是普通的JavaScript

var myImage = document.getElementById("mainImage"), 
    imageArray = ["images/1.png", "images/2.png", "images/3.png", "images/4.png"], 
    imageArrayIndex = 0; 

myImage.src = imageArray[imageArrayIndex++]; 

function changeImage() { 
    myImage.src = imageArray[imageArrayIndex++]; 
    imageArrayIndex = imageArrayIndex >= imageArray.length ? 0 : imageArrayIndex; 
} 

確保你的元素被定義爲「IMG」的解決方案。

0

下面是用於設置圖像上的data-index屬性來跟蹤所選索引中的溶液。此解決方案與IE8兼容,不使用Jquery庫。運行下面的代碼片段進行測試(點擊圖片進入下一個)。

var mimg = document.getElementById('main-image'), 
 
    simg = document.getElementById('sec-image') 
 
    imgArr = [ 
 
     'http://placehold.it/50x50/00AAAA', 
 
     'http://placehold.it/50x50/AAAA00', 
 
     'http://placehold.it/50x50/AA00AA', 
 
    ]; 
 
     
 
var loopImages = function(element, imgArray, startAt) { 
 
    var index = element.getAttribute('data-index'), 
 
     newIndex = 0; 
 
     
 
    if (!index) 
 
     newIndex = ((startAt && startAt < imgArr.length-1) || 0) + 1; 
 
    else if (index < imgArr.length-1) 
 
     newIndex = parseInt(index) + 1; 
 
    
 
    element.setAttribute('data-index', newIndex); 
 
    element.src = imgArr[newIndex]; 
 
}; 
 
     
 
mimg.addEventListener('click', function(e) { 
 
    loopImages(e.target || e.srcElement, imgArr); 
 
}); 
 
setInterval(function() { 
 
    loopImages(simg, imgArr); 
 
}, 500);
<p>Preview (click to change)</p> 
 
<img id="main-image" src="http://placehold.it/50x50/00AAAA"> 
 
<br> 
 
<p>Preview with interval</p> 
 
<img id="sec-image" src="http://placehold.it/50x50/00AAAA">

+0

感謝您的幫助但可以添加評論嗎?因爲我試圖更多地理解代碼,並且我不明白此代碼中的所有內容 – sp000142

+0

@ sp000142這很好理解。如果您不明白像'addEventListener'或'parseInt'這樣的函數,或者像'e.target'這樣的屬性,請參考[MDN文檔](https://developer.mozilla.org/en-US/docs/)網絡/ API)。 – Tyblitz

+0

這個功能比其他答案更先進和靈活,因爲1)它可以在任何元素上設置,2)它可以用於任何事件/功能,3)你可以選擇陣列中首先顯示的圖像(例如,從第二個位置開始)以及4)它不會污染具有全局變量的窗口命名空間。 – Tyblitz