[英]Javascript: Changing an image on click and then back to original when clicked again
Hello I have been looking around and I can't seem to find what I'm looking for.您好,我一直在四处寻找,但似乎找不到我要找的东西。 I have tried a few solutions suggested on other posts which have not worked in my favor.
我尝试了其他帖子中建议的一些解决方案,但对我不利。
The title pretty much sums up what I am trying to accomplish, If you need more information leave a comment asking for what you would like to know.标题几乎总结了我想要完成的事情,如果您需要更多信息,请发表评论询问您想知道的内容。
The main part of this code I want to get working is the myFunction, the rest are examples I made so that people can better understand my thought process, here is the code:我想要开始工作的这段代码的主要部分是 myFunction,rest 是我制作的示例,以便人们可以更好地理解我的思维过程,这里是代码:
function myFunction() { var demoStyle = document.getElementById("demo").style.backgroundImage; var blue = "url(http://www.babybedding.com/images/fabric/solid-turquoise-fabric_smaller.jpg)"; var red = "url(http://www.babybedding.com/images/fabric/solid-coral-fabric_smaller.jpg)"; var x = (demoStyle == blue); if (demoStyle == blue) { demoStyle = blue; document.getElementById("demo").innerHTML = "Should have turned Blue"; alert('Is the document Blue? Javascript says: ' + x + '.'); } else { demoStyle = red; document.getElementById("demo").innerHTML = "Should have turned Red"; alert('Is the document Red? Javascript says: ' + x + '.'); } } function myFunction_NoVars() { if (document.getElementById("demoNoVars").style.backgroundImage == "url(http://www.babybedding.com/images/fabric/solid-turquoise-fabric_smaller.jpg)") { document.getElementById("demoNoVars").style.backgroundImage = "url(http://www.babybedding.com/images/fabric/solid-turquoise-fabric_smaller.jpg)"; document.getElementById("demoNoVars").innerHTML = "Should have turned Blue"; } else { document.getElementById("demoNoVars").style.backgroundImage == "url(http://www.babybedding.com/images/fabric/solid-coral-fabric_smaller.jpg)" document.getElementById("demoNoVars").innerHTML = "Should have turned Red"; } } function my_2nd_Function() { document.getElementById("demo2").style.backgroundImage = "url(http://www.babybedding.com/images/fabric/solid-coral-fabric_smaller.jpg)"; document.getElementById("demo2").innerHTML = "Should have turned Red"; }
div {text-align:center;padding:5px;height:100px;width:100px;background-image:url(http://www.babybedding.com/images/fabric/solid-turquoise-fabric_smaller.jpg);} button {margin-top:10px;padding:5px;min-width:50px;} h2 {margin:0;padding:0;} hr {margin-bottom:10px;padding:0;}
<h2>Has Errors:</h2><hr /> <div id="demo"></div> <button onclick="myFunction()">Run</button> <br /><br /> <div id="demoNoVars"></div> <button onclick="myFunction_NoVars()">Run w/ No Variables</button> <br /><br /> <h2>Doesn't Have Errors:</h2><hr /> <div id="demo2"></div> <button onclick="my_2nd_Function()">Run</button> <p><strong>NOTE:</strong> This one changes, but I'm trying to create something that will change between the two every time the button is clicked.</p>
There is also a live preview here , JSfiddle here .这里还有一个实时预览,这里是JSfiddle 。 Thank you for any help.
感谢您的任何帮助。
If you are using jQuery, this is simple example, you can replace the background color to image 如果您使用的是jQuery,这是一个简单的示例,则可以将背景色替换为图片
$( "div" ).click(function() { $( this ).toggleClass( "change" ); });
#box{ background-color:red; width:300px; height:300px; } #box.change{ background-color:green; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="box" class=""></div>
There is no need for all that code. 不需要所有这些代码。 All you need to two CSS rules and to toggle a class name.
您只需要两个CSS规则并切换一个类名。 The one CSS rule has the one image and the other rule has the other.
一个CSS规则具有一个图像,另一规则具有另一个图像。 You add the class and the second image appears.
您添加了班级,第二个图像出现。
var btns = document.getElementsByTagName("button"); for (var i = 0; i < btns.length; i++) { btns[i].addEventListener("click", function() { document.getElementById(this.dataset.for).classList.toggle("active"); }); }
div.x { height: 100px; width:100px; background-image:url(http://www.babybedding.com/images/fabric/solid-turquoise-fabric_smaller.jpg); } div.x.active { background-image: url(http://www.babybedding.com/images/fabric/solid-coral-fabric_smaller.jpg); }
<div class="x" id="demo"></div> <button data-for="demo">Run</button> <div class="x" id="demo2"></div> <button data-for="demo2">Run</button>
The first function doesn't set the background image. 第一个功能不设置背景图像。 So it's not surprising it isn't changing.
因此,它没有改变也就不足为奇了。 The second function checks if it's blue and if so, sets it to blue.
第二个功能检查它是否为蓝色,如果是,则将其设置为蓝色。 The second function doesn't change it, it simply confirms existing state.
第二个功能不会更改它,它只是确认现有状态。
if (document.getElementById("demoNoVars").style.backgroundImage == "url(http://www.babybedding.com/images/fabric/solid-turquoise-fabric_smaller.jpg)") {
document.getElementById("demoNoVars").style.backgroundImage = "url(http://www.babybedding.com/images/fabric/solid-turquoise-fabric_smaller.jpg)";
has the same url so nothing changes. 具有相同的网址,因此没有任何变化。
You should be swapping your if conditions in the second function and the first function should be setting the background. 您应该交换第二个功能中的if条件,而第一个功能应设置背景。
Here's a really simple script, I've used a data-attribute to store you second image, I grab these into an array and rotate. 这是一个非常简单的脚本,我使用了一个数据属性来存储第二张图像,然后将它们抓取到一个数组中并旋转。
var imgs = document.querySelectorAll('img'); [].slice.apply(document.querySelectorAll('img')).forEach(function (f) { var im = [ f.getAttribute("src"), f.getAttribute("data-src2") ]; f.onclick = function () { var k = im.shift(1); im.push(k); f.setAttribute("src", im[0]); }; });
<img src="http://www.babybedding.com/images/fabric/solid-turquoise-fabric_smaller.jpg" data-src2="http://www.babybedding.com/images/fabric/solid-coral-fabric_smaller.jpg"> <img src="http://www.babybedding.com/images/fabric/solid-turquoise-fabric_smaller.jpg" data-src2="http://www.babybedding.com/images/fabric/solid-coral-fabric_smaller.jpg"> <img src="http://www.babybedding.com/images/fabric/solid-turquoise-fabric_smaller.jpg" data-src2="http://www.babybedding.com/images/fabric/solid-coral-fabric_smaller.jpg">
I had a few issues when moving some of these answers to my environment, but using them to do more research I found something that worked for me. 将其中一些答案移至我的环境时,我遇到了一些问题,但是使用它们进行更多研究后,我发现了一些对我有用的东西。
I want to post the solution here just incase someone comes across this question and the other solutions didn't work in their environments either. 我想在这里发布解决方案,以防万一有人遇到这个问题,而其他解决方案在他们的环境中也不起作用。
Here is the code: 这是代码:
function myFunction() { var x = document.getElementById("demo"); if (x.className.indexOf("red") == -1) { x.className = x.className.replace("blue", " red"); } else { x.className = x.className.replace("red", " blue"); } }
.blue { height: 100px; width:100px; background-image:url(http://www.babybedding.com/images/fabric/solid-turquoise-fabric_smaller.jpg); } .red { height: 100px; width:100px; background-image: url(http://www.babybedding.com/images/fabric/solid-coral-fabric_smaller.jpg); }
<div id="demo" class="blue"></div> <button onclick="myFunction()" >Button</button>
Thanks everyone for the responses! 感谢大家的回应!
let x = document.getElementById("image1"); let state = false; function transition() { x.src = state? "image.jpg": "image2.jpg"; state =;state; }
body { display: grid; justify-content: center; align-items: center; } img { cursor: pointer; width: 10em; height: 10em; }
<.DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>replit</title> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <img src="image.jpg" id="image1" onclick="transition()"> <script src="script.js"></script> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.