简体   繁体   English

每次单击按钮时,是否可以更改背景颜色并以纯JavaScript变回原始颜色?

[英]Is it possible to change the background color and back to the original in plain javascript every time i click a button?

除非我使用jQuery,否则我似乎看不到Google上的任何结果,而且我不确定jQuery和javascript是否可以协同工作。

Try using data-* attribute to store color replacement, original background color of button element or initial setting; 尝试使用data-*属性存储颜色替换, button元素的原始背景颜色或initial设置; JSON.parse() , JSON.stringify() , Array.prototype.reverse() to toggle data-* attribute values that set background of input type="button" element; JSON.parse()JSON.stringify()Array.prototype.reverse()切换设置input type="button"元素backgrounddata-*属性值; onclick event. onclick事件。

Not certain if requirement is to toggle background color, or reset to original color once ? 不确定是否需要切换背景颜色或一次重置为原始颜色?

 <input type="button" value="button" data-colors='["blue", "buttonface"]' onclick="this.style.background = JSON.parse(this.dataset.colors)[0]; this.dataset.colors = JSON.stringify(JSON.parse(this.dataset.colors).reverse());" /> 

jQuery will not conflict with javascript, it is just a js library :) So you can use the selector.css() of jquery or, in standard js i think it is (element).style.background = color . jQuery不会与javascript冲突,它只是一个js库:)因此,您可以使用jquery的selector.css() ,或者在标准js中,我认为它是(element).style.background = color

I recommend using jQuery. 我建议使用jQuery。 Here is a SIMPLE jQuery way to do it: 这是一种简单的jQuery方式:

  $("#button").click(function(){ 
   $("#tochange").css("background-color", "colorhex");
  });

If you want to swap colors you could, for example, add a variable. 如果要交换颜色,则可以添加一个变量。

var x;
$("#button").click(function(){
if(x==0){
    $("#tochange").css("background-color", "colorhex"); 
    x= 1;
 } 
 else{other way round} 
});

暂无
暂无

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

相关问题 我有一个按钮,我需要在第一次单击时更改颜色并在第二次单击时更改回原始颜色 - I have a button I need to turn color on fist click and change back to original color on second click 如何制作一个每次点击都会改变背景颜色的循环? - How to make a cycle that change the background color every time I click on it? 每次点击时更改按钮颜色 [Javascript] - Change Button color on every Click [ Javascript ] 我想更改一次按钮的背景色,一旦我单击另一个按钮就再次更改为原始颜色 - i want to change the background color of a button once clicked and change again to original color once i click another button 如何通过点击javascript中的按钮更改背景颜色和文本颜色? - How to change the background color and text color with the click of a button in javascript? 尝试将汉堡按钮的背景颜色更改为其他颜色,然后再次单击将其更改为与取消单击相同的颜色 - Trying do change background color of the hamburger button to other color, and click again it will change it to back to same color as unclick 将JSON载入div并同时更改按钮单击的背景颜色 - Loading JSON into a div and change background color on button click at the same time 使用javascript单击按钮更改元素的背景颜色样式 - Change element's background-color style on button click with javascript 使用JavaScript / ajax在单击按钮上更改特定TD的背景颜色 - Change a specific TD's background color on click button with JavaScript/ajax 我想创建一个计数器,每次单击 javascript 中的按钮时都会更改 - i want to create a counter which change every time i click on the button in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM