更改图像按钮

时间:2017-01-28 23:56:12

标签: javascript button onclick

我想要一个带有ID标签的来回更改图像按钮,这样我的html5就可以识别应该重定向JavaScript的位置。

我需要这种东西。

function changeImage() { if 
(document.getElementById 
("imgClickAndChange").src == "
storage/emulated/0/ 
Documents/Mp3Player/html/ 
images/Pause.jpg") { 
document.getElementById 
("imgClickAndChange").src = "/ 
storage/emulated/0/ 
Documents/Mp3Player/html/ 
images/play.jpg "; } else { 
document.getElementById 
("imgClickAndChange").src = " 
storage/emulated/0/ 
Documents/Mp3Player/html/ 
images/Pause.jpg "; } }

但它需要能够像这样来回改变图像。

var newsrc = "Play.jpg";

function changeImage() {
if ( newsrc == "Play.jpg" ) {
document.images["pic"].src = "/
storage/emulated/0/
Documents/Mp3Player/html5/
images/Pause.jpg";
document.images["pic"].alt = 
"Play";
newsrc  = "Pause.jpg";
}
else {
document.images["pic"].src = "/
storage/emulated/0/
Documents/Mp3Player/html5/ 
images/Play.jpg";
document.images["pic"].alt = 
"Pause";
newsrc  = "Play.jpg";
}
}

1 个答案:

答案 0 :(得分:0)

不确定这是否是您要找的:

var element = document.getElementById("imgClickAndChange")
var playPath = getPath('Play')
var pausePath = getPath('Pause')

function toggleImage() {
  if (element.src === playPath) element.src = pausePath
  else element.src = playPath
}

function getPath(type) {
  return '/storage/emulated/0/Documents/Mp3Player/html5/images/' + type + '.jpg'
}