简体   繁体   English

如何更改w3.css按钮自己的颜色?

[英]How to change w3.css button's own color?

  <link type="text/css" href=" https://www.w3schools.com/w3css/4/w3.css" rel="stylesheet"> <div class="w3-bar w3-red"> <a class="w3-bar-item w3-button w3-hover-blue">Button</a> </div> 

I want to make that if you click on it, it changes its own color. 我想这样做,如果你点击它,它会改变自己的颜色。 And if you click it again, it changes to the original color. 如果再次单击它,它将更改为原始颜色。 (see the hover) I don't know how to do, because it's a class, and the color is in the bar. (见悬停)我不知道该怎么做,因为它是一个类,颜色在栏中。

You can try this: 你可以试试这个:

 var IsCustomBG=false; $(".w3-button").click(function(){ if(IsCustomBG==false){ $(this).css("background-color","red"); IsCustomBG=true; } else{ $(this).css("background-color",""); IsCustomBG=false; } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link type="text/css" href=" https://www.w3schools.com/w3css/4/w3.css" rel="stylesheet"> <div class="w3-bar w3-red"> <a class="w3-bar-item w3-button w3-hover-blue">Button</a> </div> 

You can add/remove class on click of button. 您可以点击按钮添加/删除课程。

 $(".w3-bar-item").on("click",function(){ if($(this).hasClass('color')){ $(this).removeClass('color'); } else{ $(this).addClass('color'); } }); 
 .color{ color:green !important; background-color:yellow !important; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <link type="text/css" href=" https://www.w3schools.com/w3css/4/w3.css" rel="stylesheet"> <div class="w3-bar w3-red"> <a class="w3-bar-item w3-button w3-hover-blue">Button</a> </div> 

You can simply create a class and toggle the class on click. 您只需创建一个类并在单击时切换该类。 This approach is useful when you want to add some other css properties in future for same operation. 当您希望将来为同一操作添加一些其他css属性时,此方法很有用。

 $(".w3-button").click(function(){ $(this).toggleClass('redCls'); }); 
 .redCls{ background-color:red !important; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link type="text/css" href=" https://www.w3schools.com/w3css/4/w3.css" rel="stylesheet"> <div class="w3-bar w3-red"> <a class="w3-bar-item w3-button w3-hover-blue">Button</a> </div> 

Do this, add an ID to the link, best would be to not use CSS frameworks like these at all. 这样做,在链接中添加一个ID,最好不要使用像这样的CSS框架。

var theLink = document.querySelector("#fkbtn");
theLink.addEventListener("click", function() {

this.className = ('clicked'); this.className =('clicked'); }); });

In the Css, you need this: 在Css中,你需要这个:

.clicked{ background:black; !important;}

With frameworks, things get messy, you need the !important to override stuff, just a huge mess, the above removes all other classes and thus, the dimensions, but your link color will change. 使用框架,事情变得混乱,你需要重要的东西来覆盖东西,只是一个巨大的混乱,上面删除所有其他类,因此,尺寸,但你的链接颜色将改变。

 #check{ display: none; } label[for=check]{ padding: 5px 10px; background-color: skyblue; cursor: pointer; color: white; } #check:checked + label{ background-color: pink; } 
 <input type="checkbox" id="check" /> <label for="check">button</label> 

If you want to change button's color when you click, I think you should use checkbox trick. 如果你想在点击时更改按钮的颜色,我认为你应该使用复选框技巧。 but it's not correct answer. 但这不是正确的答案。 It's just trick. 这只是骗局。

Store the original value of the "button" and toggle between that and a custom color on click. 存储“按钮”的原始值,并在单击时在该按钮和自定义颜色之间切换。

 var button = document.getElementsByTagName('a')[0]; var color = button.style.backgroundColor; button.addEventListener('click', function(){ this.style.backgroundColor = this.style.backgroundColor === color ? '#BADA55' : color; }); 
 <link type="text/css" href=" https://www.w3schools.com/w3css/4/w3.css" rel="stylesheet"> <div class="w3-bar w3-red"> <a class="w3-bar-item w3-button w3-hover-blue">Button</a> </div> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM