我已经创建了四个单独的图片,当点击时会更换图片。

huangapple go评论81阅读模式
英文:

I have crated four individual images that when clicked change images

问题

以下是您要翻译的部分:

我在index.html中有4个独立的图像,每个图像都有4个不同的ID,并创建4个单独的函数,用于切换到另一张图像。然后,当再次点击时,我希望恢复到原始图像。希望这样说得清楚。

当用户点击前面的书籍图像时,它会切换到书的背面,我需要做的是,如果用户再次点击它,它会回到书的正面图像。

英文:

I have 4 individaula images in index.html that have 4 sepereate i.d and create 4 indiviual functions that that switch to another image. I then want to revert back to original images when clicked again. Hope that makes sense.

When the user clicks the front book image it switches to the back of the book, what i need to do is then if the user clicks again it goes back to the front of the book image.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function changeImageFour(image) {
  if (image.src = &#39;images/pink4back.jpg&#39;);
}

function changeImageThree(image) {
  image.src = &#39;images/pink3back.jpg&#39;;
}

function changeImageTwo(image) {
  image.src = &#39;images/pink2back.jpg&#39;;
}

function changeImageOne(image) {
  image.src = &#39;images/pink1back.jpg&#39;;
}

<!-- language: lang-html -->

&lt;div class=&quot;col-lg-3 text-center&quot;&gt;
  &lt;img id=&quot;pbook&quot; onclick=&quot;changeImageOne(this)&quot; class=&quot;imagebooks&quot; src=&quot;images/pink1.jpg&quot; alt=&quot;&quot;&gt;
  &lt;a href=&quot;https://www.amazon.co.uk/Pink-Club-Emma-Bruce/dp/B09J7DQWSL&quot;&gt; 
    &lt;button class=&quot;bookbutton&quot;&gt;Buy Now&lt;/button&gt;
  &lt;/a&gt;
&lt;/div&gt;

&lt;div class=&quot;col-lg-3 text-center&quot;&gt;
  &lt;img id=&quot;pbook&quot; onclick=&quot;changeImageTwo(this)&quot; class=&quot;imagebooks&quot; src=&quot;images/pink2.jpg&quot; alt=&quot;&quot;&gt;
  &lt;a href=&quot;https://www.amazon.co.uk/Dreams-Pink-Club-Emma-Bruce/dp/B0BHMRQR2Q&quot;&gt; 
    &lt;button class=&quot;bookbutton&quot;&gt;Buy Now&lt;/button&gt;
  &lt;/a&gt;
&lt;/div&gt;

&lt;div class=&quot;col-lg-3 text-center&quot;&gt;
  &lt;img id=&quot;pbook&quot; onclick=&quot;changeImageThree(this)&quot; class=&quot;imagebooks&quot; src=&quot;images/pink3.jpg&quot; alt=&quot;&quot;&gt;
  &lt;a href=&quot;https://www.amazon.co.uk/Pink-Passions-Club-Emma-Bruce/dp/B0BSY7KC5R &quot;&gt; 
    &lt;button class=&quot;bookbutton&quot;&gt;Buy Now&lt;/button&gt;
  &lt;/a&gt;
&lt;/div&gt;

&lt;div class=&quot;col-lg-3 text-center&quot;&gt;
  &lt;img id=&quot;myImage4&quot; onclick=&quot;changeImageFour(this)&quot; class=&quot;imagebooks text-center&quot; src=&quot;images/pink4.jpg&quot; alt=&quot;&quot;&gt;
  &lt;a href=&quot;https://www.amazon.co.uk/United-Pink-Club-Book-ebook/dp/B0C5P7WDFT&quot;&gt; 
  &lt;button class=&quot;bookbutton&quot;&gt; But Now&lt;/button&gt;
  &lt;/a&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 0

你已经返回了带有按钮的图像,因此在更改图像之前,您可以轻松地检查名称,例如在图像4中,可以像这样:

function changeImageFour(image) {
    if (image.src.endsWith('pink4.jpg')) {
        image.src = 'images/pink4back.jpg';
    } else {
        image.src = 'images/pink4.jpg';
    }
}
英文:

You are already returning the image with button so you could easily check the name before changing the image for example in image 4 it could be something like this

function changeImageFour(image) {



if (image.src.endsWith(&#39;pink4.jpg&#39;)) {
    image.src = &#39;images/pink4back.jpg&#39;;
  } else {
    image.src = &#39;images/pink4.jpg&#39;;
  }
}

答案2

得分: 0

以下是翻译好的部分:

  • 避免使用内联事件处理程序,例如 onclick。当 JavaScript 刚开始时,它们是添加事件侦听器的首选方式,但现在已经被addEventListener替代了一段时间。我建议您从现在开始使用 addEventListener

  • ID 必须是唯一的。它们的功能是通过唯一标识符来识别元素。只能一次使用相同的 ID。

  • 您可以将按钮嵌套在锚(<a>)标记内,但最好不要这样做。锚应该用于导航,按钮用于功能。浏览器可以单独处理它们中的每一个,但将它们嵌套在一起会让人感到困惑(它是链接还是按钮?)。如果您希望锚看起来像按钮,那么CSS是您的朋友。

关于函数方面,您不必为每个图像编写一个函数。就像 @salihkavaf 在您的另一篇帖子中所示,您可以将要显示的其他图像存储为data属性在您的图像上。单击时,切换srcdata-src属性的值。

使用JS,选择所有希望具有此行为的图像,并为每个图像添加一个单击事件侦听器。就像下面所示,这里只有一个函数用于两个图像。这是因为我们可以通过检查事件提供的信息来识别触发事件的元素。在这种情况下,event.target是对我们单击的<img/>元素的引用。在这里执行src - data-src切换技巧,所有您的图像都将正常工作。

英文:

There are a couple of important things to mention:

  • Avoid inline event handlers like onclick. They were the go-to way for adding event listeners when JavaScript was just beginning, but have been superseded by addEventListener for a while now. I'd recommend that you use addEventListener from now on.
  • ID's have to be unique. Their function is to identify elements by a unique identifier. Use the same ID only once.
  • You can nest a button inside of an achor (&lt;a&gt;) tag, but you shouldn't. Anchors should be used for navigation and buttons for functionality. Browsers know what to do with each of them individually, but nesting them makes it confusing (is it a link? Or is it a button?). If you want an anchor to look like a button, then CSS is your friend.

That being said, you don't have to write a function for each image. Like @salihkavaf showed in your other post, you can store the other image you want to show as a data attribute on your image. When clicking, switch the values from the src and data-src attribute.

With JS, select all the images that you want to have this behavior and add a click event listener for each one of them. Like you see below, there is just one function for both images. That is because with events we can identify the element that fired the event by checking the information that the event provides us. In this case event.target is a reference to the &lt;img/&gt; element that we clicked. Do the src - data-src switch trick here and all your images will work.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const images = document.querySelectorAll(&#39;.imagebooks&#39;);

function changeImage(event) {
  const image = event.target;
  const newImage = image.dataset.src;
  image.dataset.src = image.src;
  image.src = newImage;
  
  event.preventDefault();
}

for (const image of images) {
  image.addEventListener(&#39;click&#39;, changeImage);
}

<!-- language: lang-css -->

.imagebooks {
  cursor: pointer;
}

<!-- language: lang-html -->

&lt;div class=&quot;col-lg-3 text-center&quot;&gt;
  &lt;img class=&quot;imagebooks&quot; src=&quot;https://picsum.photos/200/300&quot; data-src=&quot;https://picsum.photos/300/300&quot; alt=&quot;&quot;&gt;
  
  &lt;a href=&quot;https://www.amazon.co.uk/Pink-Club-Emma-Bruce/dp/B09J7DQWSL&quot;&gt;Buy Now&lt;/a&gt;
&lt;/div&gt;

&lt;div class=&quot;col-lg-3 text-center&quot;&gt;
  &lt;img class=&quot;imagebooks&quot; src=&quot;https://picsum.photos/400/300&quot; data-src=&quot;https://picsum.photos/500/300&quot; alt=&quot;&quot;&gt;
  
  &lt;a href=&quot;https://www.amazon.co.uk/Dreams-Pink-Club-Emma-Bruce/dp/B0BHMRQR2Q&quot;&gt;Buy Now&lt;/a&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月29日 19:44:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357063.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定