如果在其他位置单击,请变回背景颜色 [英] Change background color back if clicked elsewhere

查看:109
本文介绍了如果在其他位置单击,请变回背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码加载div的某些内容以及更改用户单击的选项卡的颜色.我很难弄清楚一旦用户单击其他内容,如何使背景颜色变回原样.我该怎么办?

I am using this code to load some content of a div as well as change the color of the tab the user clicked on. I'm having trouble figuring out how to make the background color change back once the user clicks on something else. How would I do that?

$(function() {
        $('#tab-1').click(function() {
            var tabcontent = "<?php echo preg_replace("/\r?\n/", "\\n", addslashes($tabcontent[0]));?>";
            document.getElementById('top-tabs-content').innerHTML = tabcontent;
            $(this).css("background-color","#F7C703");
        });
    });

推荐答案

$(function() {
    $('#tab-1').click(function() {
        var tabcontent = "<?php echo preg_replace("/\r?\n/", "\\n", addslashes($tabcontent[0]));?>";
        document.getElementById('top-tabs-content').innerHTML = tabcontent;
        $('.tabclass').css("background-color","#defaultcolour");
        $(this).css("background-color","#F7C703");
    });
});

在元素上添加class,然后在将颜色应用于clicked element之前,将所有elements重置为默认颜色.

Add a class to the elements, then before applying the colour to clicked element, reset all the elements with the default colour.

这篇关于如果在其他位置单击,请变回背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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