如何通过此代码使用jQuery更改css属性 [英] How to change css property using jquery with this code

查看:67
本文介绍了如何通过此代码使用jQuery更改css属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#business"当前设置为背景:#323232;如何将其更改为#000;我单击"#business"并在关闭菜单后返回323232吗?

"#business" is currently set to background: #323232; How can I change it to #000; after I click on "#business" and back to 323232 once the menu is closed?

$(document).ready(function() {

    $("#business").click(function(){
        jQuery.fx.off = true;
        $("#businessmenu").toggle("");
    });

    $('html').click(function() {
        $("#businessmenu").hide("");
    });

    $('#business').click(function(event){
        event.stopPropagation();
    });

});

这是html:

<a href="#" id="business">Biz name</a>
<div id="businessmenu">
    <a href="help.html">Help</a>
</div>

推荐答案

您可以使用 css 更改CSS属性(或如有必要,可以更改多个属性)的方法:

You can use the css method to change a CSS property (or multiple properties if necessary):

$("#business").click(function(event){
    jQuery.fx.off = true;
    $("#businessmenu").toggle("");
    $(this).css("background-color", "#000");
    event.stopPropagation();
});
$('html').click(function() {
    $("#businessmenu").hide();
    $("#business").css("background-color", "#323232");
});

请注意,我已经绑定了绑定到#business的2个事件侦听器,因为仅绑定一个就没有区别.

Note that I've combined the 2 event listeners you had bound to #business as it's makes no difference to just bind the one.

作为旁注,您是否有理由将空字符串传递给hide?没必要.

As a side note, is there a reason you are passing an empty string to hide? That shouldn't be necessary.

这篇关于如何通过此代码使用jQuery更改css属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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