开发者

jquery, how to remove button colour after it has been pressed?

开发者 https://www.devze.com 2023-01-27 00:29 出处:网络
<button style=\"background-color:lightblue\" id=\"extractv开发者_开发知识库\"> <b>
<button style="background-color:lightblue" id="extractv开发者_开发知识库">
     <b>
       Extract<br>v
     </b>
</button>      
...

$("#extractv").click(function () {
   $("#extractv").removeAttr("style"); 
});

After someone clicks on the button, I want to remove the background color on the button. I am not sure why it's not working.

Thanks

Gordon


try using

.css('background-color','');


This?

<script type="text/javascript">

$(function() {
    $("#extractv").click(function () {
       $(this).css('background-color', ''); 
    });
});

</script>


I'm not exactly sure why the code you posted doesn't work but the following will do the trick

$(document).ready(function() {
    $('#extractv').click(function() {
       $(this).removeAttr('style');
    }); 

});

jsfiddle version: http://jsfiddle.net/UUxHJ/

As others have pointed out though it's better (more future-proof) here to only remove the background-color versus completely taking out the style attribute.

$('#extractv').click(function() {
   $(this).css('background-color','');
}); 


$("#extractv").css("background-color","");

http://api.jquery.com/css/


A better way to do this is to assign the color to a class, then use jQuery to add and remove the class at will:

# CSS
.colorClass {
  background-color: lightblue
}

// js
$('#extractv').click(function(){
  $(this).toggleClass('colorClass');
});

This way, you can work with whatever style features you want, without changing any javascript!


//BUTTON = the id name of your button
BUTTON.style.backgroundColor = "";
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号