簡體   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