简体   繁体   English

如何在 Jquery 中以智能方式改进此代码以更改 DIV 的背景代码

[英]How to improve this code in intelligente way in Jquery to change the background code of DIV

I've 6 div's when the user clicks on the DIV it displays the contect of the divs.当用户单击 DIV 时,我有 6 个 div,它显示 div 的内容。 (I'm using ASPX - Ajaxtabpanel). (我正在使用 ASPX - Ajaxtabpanel)。

Below I figured out the div names and what I do below is, when the click on a DIV, change the Background color.下面我找出了 div 的名称,我在下面做的是,当单击一个 DIV 时,更改背景颜色。 So Active div get another color.所以 Active div 得到另一种颜色。 Below I've JQuery code it works but I'm sure it can be done in better way.. what can I do to improve the code to use better functions of Jquery?下面我有 JQuery 代码它可以工作,但我确信它可以以更好的方式完成.. 我可以做些什么来改进代码以使用 Jquery 的更好功能?

As you probably can see, all the DIV names start with same convention... Just these middle words are different Algemeen, Juridisch, Fiscaal, Economisch, Veiligheid...正如您可能看到的,所有 DIV 名称都以相同的约定开头......只是这些中间词是不同的 Algemeen、Juridisch、Fiscaal、Economisch、Veiligheid......

Please advice how can I improve the code....请建议我如何改进代码....

<script language="javascript" type="text/javascript">

function resetColor() {
//set all background colors of Div to blue
    $("#dnn_ctr8192_ViewWebShop_TabContainer1_TabPanelAlgemeen_tab .ajax__tab_outer").css("background-color", "#edf2fb");
    $("#dnn_ctr8192_ViewWebShop_TabContainer1_TabPanelJuridisch_tab .ajax__tab_outer").css("background-color", "#edf2fb");
    $("#dnn_ctr8192_ViewWebShop_TabContainer1_TabPanelSociaal_tab .ajax__tab_outer").css("background-color", "#edf2fb");
    $("#dnn_ctr8192_ViewWebShop_TabContainer1_TabPanelFiscaal_tab .ajax__tab_outer").css("background-color", "#edf2fb");
    $("#dnn_ctr8192_ViewWebShop_TabContainer1_TabPanelEconomisch_tab .ajax__tab_outer").css("background-color", "#edf2fb");
    $("#dnn_ctr8192_ViewWebShop_TabContainer1_TabPanelVeiligheid_tab .ajax__tab_outer").css("background-color", "#edf2fb");
}

$(document).ready(function() {

  //Change the ACTIVE div background color

    $("#dnn_ctr8192_ViewWebShop_TabContainer1_TabPanelAlgemeen_tab").click(function() {
        resetColor();
        $("#dnn_ctr8192_ViewWebShop_TabContainer1_TabPanelAlgemeen_tab .ajax__tab_outer").css("background-color", "#80FE80");
    });
    $("#dnn_ctr8192_ViewWebShop_TabContainer1_TabPanelJuridisch_tab").click(function() {
    resetColor();
        $("#dnn_ctr8192_ViewWebShop_TabContainer1_TabPanelJuridisch_tab .ajax__tab_outer").css("background-color", "#80FE80");
    });
    $("#dnn_ctr8192_ViewWebShop_TabContainer1_TabPanelSociaal_tab").click(function() {
    resetColor();
        $("#dnn_ctr8192_ViewWebShop_TabContainer1_TabPanelSociaal_tab .ajax__tab_outer").css("background-color", "#80FE80");
    });
    $("#dnn_ctr8192_ViewWebShop_TabContainer1_TabPanelFiscaal_tab").click(function() {
    resetColor();
        $("#dnn_ctr8192_ViewWebShop_TabContainer1_TabPanelFiscaal_tab .ajax__tab_outer").css("background-color", "#80FE80");
    });
    $("#dnn_ctr8192_ViewWebShop_TabContainer1_TabPanelEconomisch_tab").click(function() {
    resetColor();
        $("#dnn_ctr8192_ViewWebShop_TabContainer1_TabPanelEconomisch_tab .ajax__tab_outer").css("background-color", "#80FE80");
    });
    $("#dnn_ctr8192_ViewWebShop_TabContainer1_TabPanelVeiligheid_tab").click(function() {
    resetColor();
        $("#dnn_ctr8192_ViewWebShop_TabContainer1_TabPanelVeiligheid_tab .ajax__tab_outer").css("background-color", "#80FE80");
    });
});

$(function () {
        $("span[id$='_tab']", "#<%= TabContainer1.ClientID %>").click(
        function () {
            $(".ajax__tab_outer", "#<%= TabContainer1.ClientID %>").css("background-color", "#edf2fb");
            $(".ajax__tab_outer", this).css("background-color", "#80FE80");
        });
    });

(fixed) (固定的)

I'd move away from AjaxToolkit and replace everything with jQuery.我会离开 AjaxToolkit 并用 jQuery 替换所有东西。 Add custom class to generated controls and bind events to that class.将自定义 class 添加到生成的控件并将事件绑定到该 class。 You can then use "this" keyword to work with an element which has been clicked.然后,您可以使用“this”关键字来处理已单击的元素。

EDIT:编辑:

Assign a class to all your tab containers.将 class 分配给所有选项卡容器。

class="my_class"类=“我的班级”

Then in event binding add $('.my_class').live('click', myFunctionToChangeColours);然后在事件绑定中添加$('.my_class').live('click', myFunctionToChangeColours); It's up to you whether you need to do a.live or.bind.是否需要执行 a.live 或.bind 取决于您。 After this in myFunctionToChangeColours you'd use "this" which I have mentioned before to change a colour of the tab that you have clicked on.在 myFunctionToChangeColours 之后,您将使用我之前提到的“this”来更改您单击的选项卡的颜色。

place all your selectors(on which your are firing clcik event) in side an array, then iterate that array,and bind a click event to all your selectors..that will make your code shorter.将所有选择器(在其上触发 clcik 事件)放在一个数组中,然后迭代该数组,并将单击事件绑定到所有选择器......这将使您的代码更短。 you can make your resetColor() bit shorter in this way..您可以通过这种方式使您的 resetColor() 更短一些..

function resetColor(handler){
 $("#dnn_ctr8192_ViewWebShop_TabContainer1_"+handler+".ajax__tab_outer").css("background-color", "#edf2fb");
}

and then pass that middle words to that function like this(just an example).然后像这样将中间词传递给 function (只是一个例子)。

resetColor(TabPanelVeiligheid_tab); 

hope it helps you a bit!!希望对你有一点帮助!!

If those are the only divs that use class ajax__tab_outer, you could do something like this:如果这些是唯一使用 class ajax__tab_outer 的 div,您可以执行以下操作:

<script language="javascript" type="text/javascript">
function resetColor() {
   $(".ajax__tab_outer").css("background-color", "#edf2fb");
}

function changeColor() {
   resetColor();
   $(this).find(".ajax__tab_outer").css("background-color", "#80FE80");
}

$(document).ready(function() {
   // Bind all the divs to the changeColor function
   $("#dnn_ctr8192_ViewWebShop_TabContainer1_TabPanelVeiligheid_tab").click(changeColor);
   ...
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM