简体   繁体   English

如何通过 JavaScript 更改图像来源?

[英]How do I change the source of an image via JavaScript?

So, right now, I have this code, which has an upvote and downvote image (img/upvote.png and img/downvote.png) and when they are clicked, they should change source to img/orangeupvote.png, and downvote to orangedownvote.png (which are also both images in the img folder directory).所以,现在,我有这段代码,它有一个赞成和反对的图像(img/upvote.png 和 img/downvote.png),当它们被点击时,他们应该将源更改为 img/orangeupvote.png,并反对orangedownvote.png (这也是 img 文件夹目录中的两个图像)。 So, for this I have this code:所以,为此我有这个代码:

<div style="position: fixed; margin-left: 1030px; margin-top: 235px">
    <img class="textupvote" id="textUpvoteImg" src="img/upvote.png" alt="upvote" onclick="changetextUpvote()" style="width: 100px; margin-bottom: 28px; cursor: pointer">
    <img class="textdownvote" id="textDownvoteImg" src="img/downvote.png" alt="downvote" onclick="changetextDownvote()" style="width: 120px; margin-top: -12px; position: fixed; cursor: pointer">
</div>

<script type="text/javascript">

function changetextUpvote() {
  var textUpvoteImg = document.getElementById('textUpvoteImg');
  if (textUpvoteImg.src.match("orangedownvote")) {
    textUpvoteImg.src = "img/upvote.png";
  } else {
    textUpvoteImg.src = "img/orangedownvote.png";
  }
}

function changetextDownvote() {
  var textDownvoteImg = document.getElementById('textDownvoteImg');
  if (textDownvoteImg.src.match("orangeupvote")) {
    textDownvoteImg.src = "img/downvote.png";
  } else {
    textDownvoteImg.src = "img/orangeupvote.png";
  }
}

</script>

So, when I try this out on a different document, it's working.所以,当我在不同的文档上尝试这个时,它正在工作。 The upvote and downvote images are both successfully changing to the orange versions of them (orangeupvote.png and orangedownvote.png), on click.点击时,upvote 和 downvote 图像都成功更改为橙色版本(orangeupvote.png 和 orangedownvote.png)。 However, when I try this on my current document, it isn't working.但是,当我在当前文档上尝试此操作时,它不起作用。 I even inspected, and on click, it isn't changing source, but then for any other image (anything which isn't orangeupvote.png or orangedownvote.png) is working.我什至检查过,点击后,它并没有改变源,但是对于任何其他图像(不是 orangeupvote.png 或 orangedownvote.png 的任何东西)都在工作。 If is specify any other image on click, it's working, but orangeupvote.png and orangedownvote.png aren't working.如果在单击时指定任何其他图像,它可以工作,但 orangeupvote.png 和 orangedownvote.png 不工作。

In order to write modern JavaScript you should define the Event-Listeners with addEventLisener:为了编写现代 JavaScript,您应该使用 addEventLisener 定义事件侦听器:

Also, you should not pass a function call to onclick Attribute, but only the function reference, which should be executed on click.此外,您不应将 function 调用传递给onclick属性,而应仅传递 function 引用,该引用应在单击时执行。

Correct: onclick="changetextUpvote"正确: onclick="changetextUpvote"

I'm not sure, whether the if-statements inside the Click-Handler do what they should..Maybe, I'd rather use String.prototype.includes instead of match ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes ).我不确定 Click-Handler 中的 if-statements 是否做他们应该做的事情。也许,我宁愿使用String.prototype.includes而不是 match ( https://developer.mozilla.org/en- US/docs/Web/JavaScript/Reference/Global_Objects/String/includes )。

<div style="position: fixed; margin-left: 1030px; margin-top: 235px">
    <img class="textupvote" id="textUpvoteImg" src="img/upvote.png" alt="upvote" style="width: 100px; margin-bottom: 28px; cursor: pointer">
    <img class="textdownvote" id="textDownvoteImg" src="img/downvote.png" alt="downvote" style="width: 120px; margin-top: -12px; position: fixed; cursor: pointer">
</div>

<script type="text/javascript">
  var textUpvoteImg = document.getElementById('textUpvoteImg');
  var textDownvoteImg = document.getElementById('textDownvoteImg');

function changetextUpvote() {
  if (textUpvoteImg.src.includes("orangedownvote")) {
    textUpvoteImg.src = "img/upvote.png";
  } else {
    textUpvoteImg.src = "img/orangedownvote.png";
  }
}

function changetextDownvote() {
  if (textDownvoteImg.src.includes("orangeupvote")) {
    textDownvoteImg.src = "img/downvote.png";
  } else {
    textDownvoteImg.src = "img/orangeupvote.png";
  }
}
  
// Only pass the function name, not the function call (e.g. not changetextUpvote())
textUpvoteImg.addEventListener('click', changetextUpvote);
textDownvoteImg.addEventListener('click', changetextDownvote);

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM