繁体   English   中英

按下按钮更改图像

[英]Changing images on Button press

我有这段代码可以更改我网站上的多个图像。 现在我可以更改图像一次,但我无法将它们更改回原始图像。 我试图在 my.css 之后使用 .toggle 来改变图像,但没有奏效。 我将如何解决这个问题?

  $("button").click(function(){
      $("#secriuty").css({"background-image": "url('images/certificates/secriuty-fundamentals-certificate.png')"}), 
      $("#js").css({"background-image": "url('images/certificates/js-certificate.png')"}),
      $("#html5").css({"background-image": "url('images/certificates/html5-certificate.png')"}),
      $("#html-css").css({"background-image": "url('images/certificates/html-and-css-certificate.png')"}),
      $("#photoshop").css({"background-image": "url('images/certificates/photoshop-certificate.png')"}),
      $("#illustrator").css({"background-image": "url('images/certificates/illustrator-certificate.png')"})
    });

尝试这个:

 $('.checkbox').click(function() { const button = $(this).parent('#button'); if( $(button).find('.checkbox').is(':checked') ) { $("#aaa").css({"background-image": "url('https://picsum.photos/id/141/150/150')"}) $("#bbb").css({"background-image": "url('https://picsum.photos/id/222/150/150')"}) $("#ccc").css({"background-image": "url('https://picsum.photos/id/27/150/150')"}) } else { $(".div").css({"background-image": ""}) } })
 .gallery { display: flex; flex-direction: row; } #aaa { display: block; width: 150px; height: 150px; background-image: url('https://picsum.photos/id/121/150/150'); } #bbb { display: block; width: 150px; height: 150px; background-image: url('https://picsum.photos/id/121/150/150'); } #ccc { display: block; width: 150px; height: 150px; background-image: url('https://picsum.photos/id/121/150/150'); }.div { margin-right: 5px; }.div:last-of-type { margin-right: 0; } /* Button */.btn { width: 150px; height: 40px; margin-top: 10px; background-color: #999; display: flex; align-items: center; justify-content: center; }.btn:hover { background: #444; color: #fff; }.btn.checkbox { position: absolute; left: 0; right: 0; width: 150px; height: 40px; opacity: 0; z-index: 999; cursor: pointer; -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)"; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="gallery"> <div id="aaa" class="div"></div> <div id="bbb" class="div"></div> <div id="ccc" class="div"></div> </div> <div id="button" class="btn"> <input type="checkbox" class="checkbox" /> <span>Change Background</span> </div>

使用.toggleClass

 $("button").click(function(){ $("#security").toggleClass("newpic"); });
 #security { height: 100px; width: 100px; background: red; } #security.newpic { background: url("http://www.fillmurray.com/300/253"); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="security"></div> <button>Click me</button>

JavaScript 用于更换<div>按下按钮的颜色</div><div id="text_translate"><p>当我的 html 和 JS 代码在同一个文件中时,我有点不确定为什么我的代码似乎不起作用。 当 html 和 JS 分开时,似乎工作正常。 有人可以指出我的错误....我是新手!</p><p> <strong>HTML:</strong></p><pre> &lt;div class="main"&gt; &lt;div class="light"&gt;&lt;/div&gt; &lt;button onclick="chngCol()" id="burn"&gt;Burn!&lt;/button&gt; &lt;/div&gt;</pre><p> <strong>JavaScript:</strong></p><pre> chngCol() { if(document.getElementByClass('light').style.background == "#00ffff") { document.getElementByClass('light').style.background = "#ffff00"; } else if(document.getElementByClass('light').style.background == "ffff00") { document.getElementByClass('light').style.background = "#ff00ff"; } else if(document.getElementByClass('light').style.background == "#ff00ff") { document.getElementByClass('light').style.background = "#00ffff"; } }</pre><p> <strong>CSS:</strong></p><pre> .light{ width: 50px; height: 50px; background-color:#00ffff; }</pre><p> 所有代码都在同一个文档中,带有适当的标签,但是我在第一个 { 调用 chngCol.</p></div>

[英]JavaScript for changing a <div> colour from a button press

暂无
暂无

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

相关问题 按钮按下时图像更改损坏 使用按钮在2张图像之间切换 将输入按钮更改为图像 使用按钮更改图像 JavaScript 用于更换<div>按下按钮的颜色</div><div id="text_translate"><p>当我的 html 和 JS 代码在同一个文件中时,我有点不确定为什么我的代码似乎不起作用。 当 html 和 JS 分开时,似乎工作正常。 有人可以指出我的错误....我是新手!</p><p> <strong>HTML:</strong></p><pre> &lt;div class="main"&gt; &lt;div class="light"&gt;&lt;/div&gt; &lt;button onclick="chngCol()" id="burn"&gt;Burn!&lt;/button&gt; &lt;/div&gt;</pre><p> <strong>JavaScript:</strong></p><pre> chngCol() { if(document.getElementByClass('light').style.background == "#00ffff") { document.getElementByClass('light').style.background = "#ffff00"; } else if(document.getElementByClass('light').style.background == "ffff00") { document.getElementByClass('light').style.background = "#ff00ff"; } else if(document.getElementByClass('light').style.background == "#ff00ff") { document.getElementByClass('light').style.background = "#00ffff"; } }</pre><p> <strong>CSS:</strong></p><pre> .light{ width: 50px; height: 50px; background-color:#00ffff; }</pre><p> 所有代码都在同一个文档中,带有适当的标签,但是我在第一个 { 调用 chngCol.</p></div> 按javascript的Bing的“查看更多图片按钮” 更改 <a-mixin> 按下按钮时带有javascript ID的材质 第二次按下后,Chrome扩展程序弹出按钮未更改 遍历图像,然后使用jQuery按下按钮暂停 javascript在单击按钮时在三个图像之间切换
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM