jquery 如何使按钮成功更改URL的一部分?

nwsw7zdq  于 2023-11-17  发布在  jQuery
关注(0)|答案(2)|浏览(162)

我正试图使一个网站,将使用图像的两侧2不同的按钮,我的速写本页面的扫描图像之间切换。所有的图像的URL是相同的,除了在URL的页码。我一直试图使用JavaScript,使按钮改变在URL中的数字来改变图像。
我成功地让JavaScript在图片的URL中提供了数字,然而,无论我做了什么尝试,按钮都拒绝实际更改数字。尽管感觉如此简单的事情应该很容易。
我试过各种if/else语句。我试过while/do语句。我试过“pageNumber++”和“pageNumber+=1”。我学习了JQuery的基础知识,看看它是否能以任何方式帮助我,但我意识到它解决的问题不是我遇到的问题。坦率地说,我开始感到非常沮丧。
这是我的设置减去任何按钮功能的基础:

<div> <--! This div is also a flexbox btw -->
    <button><h3>Previous</h3></button> <--! There's nothing happening with this button because I was just trying to figure out the other one first -->
 
    <a href="" id="pagelink" target="_blank"><img src="" height="400px" id="page-holder"></a>
          
    <button id="nextbutton"><h3>Next</h3></button>
</div>

<script>
let pageNumber = 1;
        
let currentPage = "https://example.com/Images/Art/Sketchbook Page " + pageNumber + ".png";
        
const pageHolder = document.getElementById('page-holder');
const pageLink = document.getElementById('pagelink');
const nextButton = document.getElementById('nextbutton');
        
pageHolder.src = currentPage;
pageLink.href = currentPage;

//Input all of the different button functions I tried here          

</script>

字符串
如果任何人有任何想法,我如何能够成功地做到这一点,这将是非常感谢。
顺便说一句,我也使用http://www.w3schools.com/lib/w3data.js来帮助在多个网站上拥有相同的html片段,而不必单独更新它们,我不认为这会干扰这一点,但我甚至不知道了:(

ygya80vv

ygya80vv1#

当你点击一个特定的元素时,你必须指定“发生什么”。因为你还没有这样做,所以你在这里写的所有JS代码都会在页面加载后立即执行。在这种情况下,你必须在你的PreviousNext按钮上添加一个EventListener,并且你必须在callback函数内部编写你的逻辑。

<div>
    <button id="prevbutton"><h3>Previous</h3></button>
 
    <a href="" id="pagelink" target="_blank">
      <img src="" height="400px" id="page-holder">
    </a>
          
    <button id="nextbutton"><h3>Next</h3></button>
</div>

<script>

let pageNumber = 1;
let imageBaseUrl = "https://example.com/Images/Art/Sketchbook Page ";   

        
const pageHolder = document.getElementById('page-holder');
const pageLink = document.getElementById('pagelink');
const previousButton = document.getElementById('prevbutton');
const nextButton = document.getElementById('nextbutton');

previousButton.addEventListener("click", function(){

  if ( pageNumber === 0 ) {
     // You may not need the pageNumber in negative numbers
     return;
  }

  pageNumber--;
  let imageUrl = imageBaseUrl + pageNumber + ".png";
  pageHolder.src = imageUrl;
  pageLink.href = imageUrl;
});

nextButton.addEventListener("click", function(){
  pageNumber++;
  let imageUrl = imageBaseUrl + pageNumber + ".png";
  pageHolder.src = imageUrl;
  pageLink.href = imageUrl;
});         

</script>

字符串

e0uiprwp

e0uiprwp2#

通过将URL存储在数组中并跟踪当前图像URL索引,可以实现在图像URL列表中的向前和向后循环。如果要将图像源更新为下一个图像,请将索引修改为下一个有效数组索引,然后更改源。若要向后,请从索引中减去。
下面是一个可重复使用的示例。它在每个步骤中都包含注解以帮助解释。此外,这里还有一些链接,指向所使用的一些API的文档:

"use strict";

// Create an array of the image URLs.
// I found these examples in a random npm package called "dog-cat-photo":
const imageUrls = [
  "cat.png",
  "cat_1600771384399.png",
  "dog.png",
  "dog_1600771291892.png",
  "dog_1600771306027.png",
].map((fileName) =>
  `https://cdn.jsdelivr.net/npm/[email protected]/${fileName}`
);

// Store the index of the current image URL.
// It is initialized to the last valid index in the array
// so that the initial update (below) will display the first image:
let currentImageIndex = imageUrls.length - 1;

// Create a reference to the image element:
const img = document.querySelector("img.current");

// Update the index by incrementing or decrementing it,
// ensuring that it stays in the valid range of array element indexes:
const changeImage = (prevOrNext) => {
  currentImageIndex = (currentImageIndex + (prevOrNext === "next" ? 1 : -1)) %
    imageUrls.length;
  if (currentImageIndex < 0) currentImageIndex += imageUrls.length;
  img.src = imageUrls[currentImageIndex];
};

// Initialize the image source to the first URL in the array:
changeImage("next");

// Bind a click event listener to the "previous" and "next" buttons,
// which invokes the function above, effectively updating the image source:
document.querySelector("button.img-control.previous")
  .addEventListener("click", () => changeImage("previous"));

document.querySelector("button.img-control.next")
  .addEventListener("click", () => changeImage("next"));
body {
  font-family: sans-serif;
  background-color: rgb(14, 14, 14);
  color: #fff;
}

#root {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.img-controls {
  display: flex;
  gap: 1rem;
}

.img-control {
  font-size: 1rem;
  padding: 0.5rem;
}
<div id="root">
  <div class="img-controls">
    <button class="img-control previous">Previous</button>
    <button class="img-control next">Next</button>
  </div>
  <div>
    <img class="current" src="" />
  </div>
</div>

相关问题