<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
Gordontry 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 = "";
精彩评论