I am having issues with Array and the element thereof

时间:2016-02-12 19:51:24

标签: javascript

I have been trying to create an array that stores String and sequentially display it after some time just like the image slideshow, I got everything right except what I don't get is I don't know please any help rendered will be greatly appreciated. This is the code:

var addressChange = ["Our Location", "Where to get us", "We reside here"];
var addressBar = document.getElementById("addressBar");
var addressIndex = 0;

function changeAddress() {
    addressBar.setAttribute("p", addressChange[addressIndex]);
    addressIndex++;
    if (addressIndex >= addressChange.length) {
        addressIndex = 0;
    }
}

2 个答案:

答案 0 :(得分:0)

Without seeing your HTML, I think you want to change the innerText of whatever element contains the strings.

Replacing the first line of your changeAddress function with this, maybe:

addressBar.innerText = addressChange[addressIndex];

Can you share your HTML to help give more context?

答案 1 :(得分:0)

I hope I understood what you want, anyways here is my shoot: https://jsfiddle.net/1ctquynr/5/

<div id = "addressBar"></div>
<script>
var number = 0;
var addressChange = ["Our Location", "Where to get us", "We reside here"];
var addressBar = document.getElementById("addressBar");

setInterval(changeArray, 5000); //change it every few seconds

function changeArray(){
    addressBar.innerHTML = addressChange[number]; //change the innerHTML according to the current array property
    number++;
    if (number === addressChange.length) //reset
    number = 0;
}
</script>