jquery,如何在按下按钮后删除按钮颜色?

发布于 2024-10-04 10:30:15 字数 313 浏览 0 评论 0原文

<button style="background-color:lightblue" id="extractv">
     <b>
       Extract<br>v
     </b>
</button>      
...

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

当有人点击按钮后,我想删除按钮上的背景颜色。我不知道为什么它不起作用。

谢谢
戈登

<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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

如梦亦如幻 2024-10-11 10:30:15

尝试使用

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

try using

.css('background-color','');
滥情空心 2024-10-11 10:30:15

这?

<script type="text/javascript">

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

</script>

This?

<script type="text/javascript">

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

</script>
梓梦 2024-10-11 10:30:15

我不太确定为什么您发布的代码不起作用,但以下代码可以解决

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

});

jsfiddle 版本的问题: http:// /jsfiddle.net/UUxHJ/

正如其他人指出的那样,尽管这里只删除背景颜色比完全删除样式属性更好(更面向未来)。

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

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','');
}); 
携余温的黄昏 2024-10-11 10:30:15
$("#extractv").css("background-color","");

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

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

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

冬天旳寂寞 2024-10-11 10:30:15

更好的方法是将颜色分配给一个类,然后使用 jQuery 随意添加和删除该类:

# CSS
.colorClass {
  background-color: lightblue
}

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

这样,您就可以使用您想要的任何样式功能,而无需更改任何 javascript!

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!

メ斷腸人バ 2024-10-11 10:30:15
//BUTTON = the id name of your button
BUTTON.style.backgroundColor = "";
//BUTTON = the id name of your button
BUTTON.style.backgroundColor = "";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文