如何避免在JavaScript中重复多个链接

发布于 2025-02-09 18:49:18 字数 3380 浏览 0 评论 0原文

我想做到这一点,以便当您按页面上的视频链接时,它将更改iframe视频的来源。

这就是现在有效的方法:

// Video links

 const videoLinks = {
  m1: "https://www.youtube-nocookie.com/embed/myvideo",
  m2: "https://www.youtube-nocookie.com/embed/myvideo",
  m3: "https://www.youtube-nocookie.com/embed/myvideo",
  m4: "https://www.youtube-nocookie.com/embed/myvideo",
  m5: "https://www.youtube-nocookie.com/embed/myvideo",
  m6: "https://www.youtube-nocookie.com/embed/myvideo",
  m7: "https://www.youtube-nocookie.com/embed/myvideo",
  m8: "https://www.youtube-nocookie.com/embed/myvideo",
  m9: "https://www.youtube-nocookie.com/embed/myvideo",
  m10: "https://www.youtube-nocookie.com/embed/myvideo",
  m11: "https://www.youtube-nocookie.com/embed/myvideo",
  m12: "https://www.youtube-nocookie.com/embed/myvideo",
  m13: "https://www.youtube-nocookie.com/embed/myvideo",
  m14: "https://www.youtube-nocookie.com/embed/myvideo",
  m15: "https://www.youtube-nocookie.com/embed/myvideo",
  m16: "https://www.youtube-nocookie.com/embed/myvideo",
  m17: "https://www.youtube-nocookie.com/embed/myvideo",
  m18: "https://www.youtube-nocookie.com/embed/myvideo",
  m19: "https://www.youtube-nocookie.com/embed/myvideo",
};

// Music playlist links

document.getElementById("m1").addEventListener("click", function () {
  musicVideo.src = videoLinks.m1;
});

document.getElementById("m2").addEventListener("click", function () {
  musicVideo.src = videoLinks.m2;
});

document.getElementById("m3").addEventListener("click", function () {
  musicVideo.src = videoLinks.m3;
});

document.getElementById("m4").addEventListener("click", function () {
  musicVideo.src = videoLinks.m4;
});

document.getElementById("m5").addEventListener("click", function () {
  musicVideo.src = videoLinks.m5;
});

document.getElementById("m6").addEventListener("click", function () {
  musicVideo.src = videoLinks.m6;
});

document.getElementById("m7").addEventListener("click", function () {
  musicVideo.src = videoLinks.m7;
});

document.getElementById("m8").addEventListener("click", function () {
  musicVideo.src = videoLinks.m8;
});

document.getElementById("m9").addEventListener("click", function () {
  musicVideo.src = videoLinks.m9;
});

document.getElementById("m10").addEventListener("click", function () {
  musicVideo.src = videoLinks.m10;
});

document.getElementById("m11").addEventListener("click", function () {
  musicVideo.src = videoLinks.m11;
});

document.getElementById("m12").addEventListener("click", function () {
  musicVideo.src = videoLinks.m12;
});

document.getElementById("m13").addEventListener("click", function () {
  musicVideo.src = videoLinks.m13;
});

document.getElementById("m14").addEventListener("click", function () {
  musicVideo.src = videoLinks.m14;
});

document.getElementById("m15").addEventListener("click", function () {
  musicVideo.src = videoLinks.m15;
});

document.getElementById("m16").addEventListener("click", function () {
  musicVideo.src = videoLinks.m16;
});

document.getElementById("m17").addEventListener("click", function () {
  musicVideo.src = videoLinks.m17;
});

document.getElementById("m18").addEventListener("click", function () {
  musicVideo.src = videoLinks.m18;
});

document.getElementById("m19").addEventListener("click", function () {
  musicVideo.src = videoLinks.m19;
});

但是它很长,我知道它可能会更短,我尝试制作循环,但我无法弄清楚如何键入它的逻辑,任何建议都会受到赞赏。

i want to make it so that when you press a video link on the page it will change the source of an iframe video.

This is what works right now:

// Video links

 const videoLinks = {
  m1: "https://www.youtube-nocookie.com/embed/myvideo",
  m2: "https://www.youtube-nocookie.com/embed/myvideo",
  m3: "https://www.youtube-nocookie.com/embed/myvideo",
  m4: "https://www.youtube-nocookie.com/embed/myvideo",
  m5: "https://www.youtube-nocookie.com/embed/myvideo",
  m6: "https://www.youtube-nocookie.com/embed/myvideo",
  m7: "https://www.youtube-nocookie.com/embed/myvideo",
  m8: "https://www.youtube-nocookie.com/embed/myvideo",
  m9: "https://www.youtube-nocookie.com/embed/myvideo",
  m10: "https://www.youtube-nocookie.com/embed/myvideo",
  m11: "https://www.youtube-nocookie.com/embed/myvideo",
  m12: "https://www.youtube-nocookie.com/embed/myvideo",
  m13: "https://www.youtube-nocookie.com/embed/myvideo",
  m14: "https://www.youtube-nocookie.com/embed/myvideo",
  m15: "https://www.youtube-nocookie.com/embed/myvideo",
  m16: "https://www.youtube-nocookie.com/embed/myvideo",
  m17: "https://www.youtube-nocookie.com/embed/myvideo",
  m18: "https://www.youtube-nocookie.com/embed/myvideo",
  m19: "https://www.youtube-nocookie.com/embed/myvideo",
};

// Music playlist links

document.getElementById("m1").addEventListener("click", function () {
  musicVideo.src = videoLinks.m1;
});

document.getElementById("m2").addEventListener("click", function () {
  musicVideo.src = videoLinks.m2;
});

document.getElementById("m3").addEventListener("click", function () {
  musicVideo.src = videoLinks.m3;
});

document.getElementById("m4").addEventListener("click", function () {
  musicVideo.src = videoLinks.m4;
});

document.getElementById("m5").addEventListener("click", function () {
  musicVideo.src = videoLinks.m5;
});

document.getElementById("m6").addEventListener("click", function () {
  musicVideo.src = videoLinks.m6;
});

document.getElementById("m7").addEventListener("click", function () {
  musicVideo.src = videoLinks.m7;
});

document.getElementById("m8").addEventListener("click", function () {
  musicVideo.src = videoLinks.m8;
});

document.getElementById("m9").addEventListener("click", function () {
  musicVideo.src = videoLinks.m9;
});

document.getElementById("m10").addEventListener("click", function () {
  musicVideo.src = videoLinks.m10;
});

document.getElementById("m11").addEventListener("click", function () {
  musicVideo.src = videoLinks.m11;
});

document.getElementById("m12").addEventListener("click", function () {
  musicVideo.src = videoLinks.m12;
});

document.getElementById("m13").addEventListener("click", function () {
  musicVideo.src = videoLinks.m13;
});

document.getElementById("m14").addEventListener("click", function () {
  musicVideo.src = videoLinks.m14;
});

document.getElementById("m15").addEventListener("click", function () {
  musicVideo.src = videoLinks.m15;
});

document.getElementById("m16").addEventListener("click", function () {
  musicVideo.src = videoLinks.m16;
});

document.getElementById("m17").addEventListener("click", function () {
  musicVideo.src = videoLinks.m17;
});

document.getElementById("m18").addEventListener("click", function () {
  musicVideo.src = videoLinks.m18;
});

document.getElementById("m19").addEventListener("click", function () {
  musicVideo.src = videoLinks.m19;
});

But it's super long and i know it can be shorter, I've tried making a for loop but i can't figure out the logic for how to type it, any suggestions is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

要走就滚别墨迹 2025-02-16 18:49:18

您可以使用这样的foreach循环。

const videoLinks = {
    m1: "https://www.youtube-nocookie.com/embed/myvideo",
    m2: "https://www.youtube-nocookie.com/embed/myvideo",
    m3: "https://www.youtube-nocookie.com/embed/myvideo",
    m4: "https://www.youtube-nocookie.com/embed/myvideo",
    m5: "https://www.youtube-nocookie.com/embed/myvideo",
    m6: "https://www.youtube-nocookie.com/embed/myvideo",
    m7: "https://www.youtube-nocookie.com/embed/myvideo",
    m8: "https://www.youtube-nocookie.com/embed/myvideo",
    m9: "https://www.youtube-nocookie.com/embed/myvideo",
    m10: "https://www.youtube-nocookie.com/embed/myvideo",
    m11: "https://www.youtube-nocookie.com/embed/myvideo",
    m12: "https://www.youtube-nocookie.com/embed/myvideo",
    m13: "https://www.youtube-nocookie.com/embed/myvideo",
    m14: "https://www.youtube-nocookie.com/embed/myvideo",
    m15: "https://www.youtube-nocookie.com/embed/myvideo",
    m16: "https://www.youtube-nocookie.com/embed/myvideo",
    m17: "https://www.youtube-nocookie.com/embed/myvideo",
    m18: "https://www.youtube-nocookie.com/embed/myvideo",
    m19: "https://www.youtube-nocookie.com/embed/myvideo",
  };

  Object.keys(videoLinks).forEach(key=>{
    document.getElementById(key).addEventListener("click", function () {
        musicVideo.src = videoLinks[key];
      });
  })

You can use forEach loop like this.

const videoLinks = {
    m1: "https://www.youtube-nocookie.com/embed/myvideo",
    m2: "https://www.youtube-nocookie.com/embed/myvideo",
    m3: "https://www.youtube-nocookie.com/embed/myvideo",
    m4: "https://www.youtube-nocookie.com/embed/myvideo",
    m5: "https://www.youtube-nocookie.com/embed/myvideo",
    m6: "https://www.youtube-nocookie.com/embed/myvideo",
    m7: "https://www.youtube-nocookie.com/embed/myvideo",
    m8: "https://www.youtube-nocookie.com/embed/myvideo",
    m9: "https://www.youtube-nocookie.com/embed/myvideo",
    m10: "https://www.youtube-nocookie.com/embed/myvideo",
    m11: "https://www.youtube-nocookie.com/embed/myvideo",
    m12: "https://www.youtube-nocookie.com/embed/myvideo",
    m13: "https://www.youtube-nocookie.com/embed/myvideo",
    m14: "https://www.youtube-nocookie.com/embed/myvideo",
    m15: "https://www.youtube-nocookie.com/embed/myvideo",
    m16: "https://www.youtube-nocookie.com/embed/myvideo",
    m17: "https://www.youtube-nocookie.com/embed/myvideo",
    m18: "https://www.youtube-nocookie.com/embed/myvideo",
    m19: "https://www.youtube-nocookie.com/embed/myvideo",
  };

  Object.keys(videoLinks).forEach(key=>{
    document.getElementById(key).addEventListener("click", function () {
        musicVideo.src = videoLinks[key];
      });
  })
抚你发端 2025-02-16 18:49:18

您应该使用数组来进行这种显然重复的觉得事物 - 还有一个基本前缀,即嵌入URL的外观,您只需要在视频认同部分(您排除在示例之外)上即可。只要您以0启动按钮ID-与数组中的索引相同 - 您就不太可能混淆哪个部分属于哪个。.它们可能也应该是由基于同一数组的循环生成的 - 所以对您的一组视频的任何更改都会立即转化为更多按钮。

const videoPrefix = 'https://www.youtube-nocookie.com/embed/';
const videoLinks = [ 'video-part-of-URL-for-index-0-number-1', 'vid-url-1-2', … ]; // the identifying part of the embed URL
const musicVideo = document.querySelector('#the-ID-of-the-player-element'); // fix this selector
for(let index = 0; index < 19; index++) // human [1 – 19], computer [0 - 18]
{
  document.querySelector("#m" + index).addEventListener("click", () => { musicVideo.src = videoPrefix + videoLinks[index]; } );
}

You should use arrays for such obviously repeating iterable things - there's also a basic prefix of what the embed URL looks like, you just need to tack on the video-identity part (which you left out of your example). As long as you start your button IDs with 0 - the same as the index inside the array - you're less likely to confuse which part belongs to which .. they should probably be generated by a loop based off the same array too - so that any changes to your set of videos immediately translates to more buttons.

const videoPrefix = 'https://www.youtube-nocookie.com/embed/';
const videoLinks = [ 'video-part-of-URL-for-index-0-number-1', 'vid-url-1-2', … ]; // the identifying part of the embed URL
const musicVideo = document.querySelector('#the-ID-of-the-player-element'); // fix this selector
for(let index = 0; index < 19; index++) // human [1 – 19], computer [0 - 18]
{
  document.querySelector("#m" + index).addEventListener("click", () => { musicVideo.src = videoPrefix + videoLinks[index]; } );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文