如何使用jQuery隐藏带有Class / id的div

时间:2022-11-25 18:10:52

I have a function openbag(); for opening div. this div is also open on click and addClass "active". but i am open this div from server side, call the function openbag();. but the problem is this div is open every time when refresh the other page. I have removed "active" class and check if class has not but this is always showing. here is my code i am using.

我有一个函数openbag();打开div。这个div也是在点击和addClass“活动”时打开的。但我从服务器端打开这个div,调用函数openbag();.但问题是每次刷新其他页面时这个div都是打开的。我已经删除了“活动”类,并检查类是否已经没有,但这总是显示。这是我正在使用的代码。

function openBag() {
   $("#bag").addClass("active");
   $("#expanding-bag").slideDown("fast");
  }


$(document).ready(function () {
    if ($("div#bag").hasClass("active")) {
        $("#expanding-bag").show();
        $("#bag").removeClass("active");
    }

    else {
        $("#expanding-bag").hide();
    }

in c# :

在c#中:

Page.ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script language='javascript'>openBag();</script>");

2 个解决方案

#1


1  

You can avoid open it and close it and all that, and direct manipulate the javascript from code behind. One trick is to use Literal and show it or not, and place inside of it the code as:

您可以避免打开它并关闭它以及所有这些,并直接操纵后面的代码中的javascript。一个技巧是使用Literal并显示或不显示,并将代码放在其中:

<asp:Literal runat="server" id="ShowIt" Visible="false" EnableViewState="false">
$(document).ready(function () {
    $("#expanding-bag").show();    
}
</asp:Literal>

<asp:Literal runat="server" id="HideIt" Visible="false" EnableViewState="false">
$(document).ready(function () {
    $("#expanding-bag").hide();
}
</asp:Literal>

and on code behind you make ShowIt.Visible=true; in the one you wish to run.

在你后面的代码上使ShowIt.Visible = true;在你想要跑的那个。

Other way is to direct render this code on page from code behind.

其他方法是从代码后面直接在页面上呈现此代码。

#2


1  

you can also try this way put your

你也可以尝试这种方式把你的

expandingbag to runat="server"

expandbag to runat =“server”

access your expanding-bag on server side and when you have to hide the control use following code

在服务器端访问您的扩展包,当您必须隐藏控件时使用以下代码

expandingbag.Style.add("display","none");

expandingbag.Style.add( “显示”, “无”);

or when you have to display then use

或者当你必须显示然后使用

expandingbag.Style.add("display","inherit");

expandingbag.Style.add( “显示”, “继承”);

#1


1  

You can avoid open it and close it and all that, and direct manipulate the javascript from code behind. One trick is to use Literal and show it or not, and place inside of it the code as:

您可以避免打开它并关闭它以及所有这些,并直接操纵后面的代码中的javascript。一个技巧是使用Literal并显示或不显示,并将代码放在其中:

<asp:Literal runat="server" id="ShowIt" Visible="false" EnableViewState="false">
$(document).ready(function () {
    $("#expanding-bag").show();    
}
</asp:Literal>

<asp:Literal runat="server" id="HideIt" Visible="false" EnableViewState="false">
$(document).ready(function () {
    $("#expanding-bag").hide();
}
</asp:Literal>

and on code behind you make ShowIt.Visible=true; in the one you wish to run.

在你后面的代码上使ShowIt.Visible = true;在你想要跑的那个。

Other way is to direct render this code on page from code behind.

其他方法是从代码后面直接在页面上呈现此代码。

#2


1  

you can also try this way put your

你也可以尝试这种方式把你的

expandingbag to runat="server"

expandbag to runat =“server”

access your expanding-bag on server side and when you have to hide the control use following code

在服务器端访问您的扩展包,当您必须隐藏控件时使用以下代码

expandingbag.Style.add("display","none");

expandingbag.Style.add( “显示”, “无”);

or when you have to display then use

或者当你必须显示然后使用

expandingbag.Style.add("display","inherit");

expandingbag.Style.add( “显示”, “继承”);