开发者

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

<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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜