Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

javascript - Using array in document.getElementById.src not working

Im trying to use an array (or var) to input into the src of an tag. when I use the text of the url the browser can display the second src but when I turn it into a var -array it doesn't seem to work:

this is my html:

    <iframe id="myFrame" src= "https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d24190.105180007897!2d-73.84374818788723!3d40.72323027714316!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x89c25e394b6cc0b9%3A0xf15c2215b49863c4!2zUmVnbyBQYXJrLCDXp9eV15XXmdeg16EsINeg15nXlSDXmdeV16jXpywg15DXqNem15XXqiDXlNeR16jXmdeq!5e0!3m2!1siw!2sil!4v1609851520165!5m2!1siw!2sil" > </iframe>

    <div class="btn-container">
        <button class="switch-buttons" id="prevbutton" >Prev Location</button> 
        <button class="switch-buttons" id="nextbutton" onclick="myFunction()">Next Location</button>
    </div>     

this is my Js:

let jlm = "https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d108513.70308822051!2d35.10531865599252!3d31.796299428569416!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x1502d7d634c1fc4b%3A0xd96f623e456ee1cb!2sJerusalem!5e0!3m2!1sen!2sil!4v1610023043184!5m2!1sen!2sil";

let rome = "https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d190029.0177339561!2d12.395913509664267!3d41.90998597323602!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x132f6196f9928ebb%3A0xb90f770693656e38!2sRome%2C%20Metropolitan%20City%20of%20Rome%2C%20Italy!5e0!3m2!1sen!2sil!4v1610023162463!5m2!1sen!2sil";

let listOfCitys = [jlm, rome];


function myFunction() {

    document.getElementById("myFrame").src = listOfCitys[0];

}

I am trying to create a loop so that each time i click the btn it will iterate the src in the array. but just putting one of the vars into the src is not working.

Thank you!

question from:https://stackoverflow.com/questions/65647626/using-array-in-document-getelementbyid-src-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Start of by tracking which location is active at the moment. This could look like:

let currentCityIndex = 0;

This way you can select a src from the listOfCitys array.

listOfCitys[currentCityIndex];

When you want to go to the next increment the index and select a new item from the array.

currentCityIndex++;
listOfCitys[currentCityIndex];

And do the same for previous with decrement.

currentCityIndex--;
listOfCitys[currentCityIndex];

Now the currentCityIndex does not know how many items the listOfCitys array has. So before incrementing or decrementing you'll have to check the current state and if the next action will be within minimum and maximum range of the array (0 and listOfCitys.length - 1).

const frame = document.getElementById("myFrame");

const prevCity = document.getElementById('prevbutton');
const nextCity = document.getElementById('nextbutton');

let jlm = "https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d108513.70308822051!2d35.10531865599252!3d31.796299428569416!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x1502d7d634c1fc4b%3A0xd96f623e456ee1cb!2sJerusalem!5e0!3m2!1sen!2sil!4v1610023043184!5m2!1sen!2sil";
let rome = "https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d190029.0177339561!2d12.395913509664267!3d41.90998597323602!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x132f6196f9928ebb%3A0xb90f770693656e38!2sRome%2C%20Metropolitan%20City%20of%20Rome%2C%20Italy!5e0!3m2!1sen!2sil!4v1610023162463!5m2!1sen!2sil";

let listOfCitys = [jlm, rome];
let currentCityIndex = 0;

prevCity.addEventListener('click', () => {
  // If the current index is already the first item..
  if (currentCityIndex === 0) {
    // ..Then go to the last..
    currentCityIndex = listOfCitys.length - 1;
  } else {
    // ..Otherwise to the previous.
    currentCityIndex--;
  }
  frame.src = listOfCitys[currentCityIndex];
});

nextCity.addEventListener('click', () => {
  // If the current index is already the last item..
  if (currentCityIndex === listOfCitys.length - 1) {
    // ..Then go to the start..
    currentCityIndex = 0;
  } else {
    // ..Otherwise to the next.
    currentCityIndex++;
  }
  frame.src = listOfCitys[currentCityIndex];
});
<iframe id="myFrame" src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d24190.105180007897!2d-73.84374818788723!3d40.72323027714316!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x89c25e394b6cc0b9%3A0xf15c2215b49863c4!2zUmVnbyBQYXJrLCDXp9eV15XXmdeg16EsINeg15nXlSDXmdeV16jXpywg15DXqNem15XXqiDXlNeR16jXmdeq!5e0!3m2!1siw!2sil!4v1609851520165!5m2!1siw!2sil"> </iframe>

<div class="btn-container">
  <button class="switch-buttons" id="prevbutton">Prev Location</button>
  <button class="switch-buttons" id="nextbutton">Next Location</button>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...