单击按钮时更改按钮的背景色 [英] Changing a button's background color when it's clicked

查看:53
本文介绍了单击按钮时更改按钮的背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JavaScript更改按钮的 background-color 属性.该脚本检查当前 background-color 的设置,然后进行切换.这是JavaScript代码:

I'm trying to change the background-color property of a button using JavaScript. The script checks what the current background-color is set to and then toggles it. This is the JavaScript code:

function btnColor(btn,color) {
    var property=document.getElementById(btn);
    if (property.style.background-color == "#f47121") {
        property.style.background-color=Color;
    }
    else {
        property.style.background-color = "#f47121";
    }
}

这就是我在html中传递的内容:

and this is what I pass in html:

<input type="button" id="btnHousing" value="Housing" onclick="toggleLayer('transparent1');btnColor('btnHousing','#fff200');" />

toggleLayer 是我正在使用的另一个功能,可以很好地工作.我似乎无法弄清楚为什么它不起作用.

toggleLayer is another function I am using, which works perfectly fine. I can't seem to figure out why it doesn't work.

推荐答案

为什么不只使用jQuery?

Why not just use jQuery?

核心Javascript太原始了!如果您只是在更改背景颜色,则在jQuery中使用on click事件:

Core Javascript is so raw! If you're just changing the background-color then use the on click event in jQuery:

$('#btnHousing').click(function() {
    //Now just reference this button and change CSS
    $(this).css('background-color','#f47121');
});

就我个人而言,它比原始javascript读起来要好得多.

Personally for me it reads so much better then raw javascript.

致谢

这篇关于单击按钮时更改按钮的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆