最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Show or Hide div from label Click - Stack Overflow

programmeradmin5浏览0评论

I have three labels having folowing code on all with different ids and three divs with different ids

<asp:Label ID="CA" runat="server"  Font-Bold="False" Font-Names="Arial" Font-Size="10pt" style="padding-top:6px;" ForeColor="#CCCCCC" Height="100%" Text="Current Activities" onmouseover="var b = ChangeColorCA(); this.style.cursor='pointer'; return b" onmouseout="RemoveColorCA()"></asp:Label>&nbsp;

here is div code for all

<div id="DIV_CA" runat=server align="center" visible="false" style="background-color:#f3f3f3; text-align: left; width: 500px; height: 470px; overflow:auto;">Some data</div>

I want to make a show or hide mechanism from label click can anyone tell me how can i do this that when i click a label then the a specific div should show and others should hide and when i click next label the its coresspondent div should show.

UPdate This is My Script Code

<script type="text/javascript">
  function hideshow(span) {
    var div = document.getElementById("DIV_" + span.id);
    if (div.style.display == "none")
       div.style.display = "block";
    else
       div.style.display = "none";
    }
</script> 

and here is lablel code

<asp:Label ID="CA" runat="server"  Font-Bold="False" Font-Names="Arial" Font-Size="10pt" style="padding-top:6px;" ForeColor="#CCCCCC" Height="100%" Text="Current Activities" onmouseover="var b = ChangeColorCA(); this.style.cursor='pointer'; return b" onmouseout="RemoveColorCA()" onclick="hideshow(this)"  ></asp:Label>&nbsp;

I have three labels having folowing code on all with different ids and three divs with different ids

<asp:Label ID="CA" runat="server"  Font-Bold="False" Font-Names="Arial" Font-Size="10pt" style="padding-top:6px;" ForeColor="#CCCCCC" Height="100%" Text="Current Activities" onmouseover="var b = ChangeColorCA(); this.style.cursor='pointer'; return b" onmouseout="RemoveColorCA()"></asp:Label>&nbsp;

here is div code for all

<div id="DIV_CA" runat=server align="center" visible="false" style="background-color:#f3f3f3; text-align: left; width: 500px; height: 470px; overflow:auto;">Some data</div>

I want to make a show or hide mechanism from label click can anyone tell me how can i do this that when i click a label then the a specific div should show and others should hide and when i click next label the its coresspondent div should show.

UPdate This is My Script Code

<script type="text/javascript">
  function hideshow(span) {
    var div = document.getElementById("DIV_" + span.id);
    if (div.style.display == "none")
       div.style.display = "block";
    else
       div.style.display = "none";
    }
</script> 

and here is lablel code

<asp:Label ID="CA" runat="server"  Font-Bold="False" Font-Names="Arial" Font-Size="10pt" style="padding-top:6px;" ForeColor="#CCCCCC" Height="100%" Text="Current Activities" onmouseover="var b = ChangeColorCA(); this.style.cursor='pointer'; return b" onmouseout="RemoveColorCA()" onclick="hideshow(this)"  ></asp:Label>&nbsp;
Share Improve this question edited Jan 7, 2012 at 10:55 Adeel Aslam asked Jan 7, 2012 at 10:16 Adeel AslamAdeel Aslam 1,29410 gold badges38 silver badges72 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

You can write JavaScript.

Markup:

<asp:Label ID="CA" 
               runat="server"
               onclick="hideshow(this)"
               Text="Label">
</asp:Label>
<div id="DIV_CA" 
     runat=server 
     align="center"  
     style="background-color:#f3f3f3; text-align:
            left; width: 500px; height: 470px; overflow:auto; display:none;">
         Some data
</div>

JavaScript:

 function hideshow(span) {
    var div = document.getElementById("DIV_" + span.id);
    if (div.style.display == "none")
       div.style.display = "block";
    else
       div.style.display = "none";
    }

EDIT: To hide all divs and show a specific div.

Markup: put all <asp:Label/> and <div> inside another <div/>

 <div id="allDiv">
        <asp:Label ID="CA" runat="server"  Font-Bold="False" Font-Names="Arial" Font-Size="10pt" style="padding-top:6px;" ForeColor="#CCCCCC" Height="100%" Text="Current Activities" onmouseover="this.style.cursor='pointer'; "   onclick="hideshow(this)"></asp:Label>
        <asp:Label ID="CB" runat="server"  Font-Bold="False" Font-Names="Arial" Font-Size="10pt" style="padding-top:6px;" ForeColor="#CCCCCC" Height="100%" Text="Current Activities" onmouseover="this.style.cursor='pointer'; "   onclick="hideshow(this)"></asp:Label>
        <div id="DIV_CA" runat="server" align="center"  style="background-color:#f3f3f3; text-align: left; width: 500px; height: 470px; overflow:auto; display:none;">Some data1</div>
        <div id="DIV_CB" runat="server" align="center"  style="background-color:#f3f3f3; text-align: left; width: 500px; height: 470px; overflow:auto; display:none;">Some data2</div>
</div>

JavaScript: function hideDiv() set display:none to all child div.

<script type="text/javascript">
    function hideshow(span) {
        hideDiv();
        span.style.fontWeight = "bold";
        var div = document.getElementById("DIV_" + span.id);
        if (div.style.display == "none")
            div.style.display = "block";
        else
            div.style.display = "none"; 
    }
    function hideDiv() {
        var childDiv = document.getElementById("allDiv").childNodes;
        for (i = 0; i < childDiv.length; i++) {
            if (childDiv[i].tagName == "DIV") {
                childDiv[i].style.display = "none";
            }
            if (childDiv[i].tagName == "SPAN") {
                childDiv[i].style.fontWeight = "normal";
            }
        }
    }
</script>

use this way in JQuery:

$("#<%= CA.ClientID %>").click(function(){

    $("#<%= DIV_CA.ClientID %>").toggle();

});

1) Create two CSS classes; divClass and divVisible. One is for all the divs and the other is for the visible div.

2) Register an OnClientClick event on the Label(s) with a reference to a JavaScript function; showHideDiv.

3) In the JavaScript function, hide all divs with the visible class and use the ID of the Label to find and show the correct div.

function showHideDiv(el)
{
  var id = el.getAttribute('id');
  var div = document.getElementById('DIV_' + id);
  var hideVisibleDiv = document.getElementsByClassName('divVisible');

  for(var e in hideVisibleDiv)
  {
    hideVisibleDivs[e].className = 'divClass';
  }
  div.className = 'divClass divVisible';
}

Something like that...

It will be works fine :

<asp:Label CssClass="clickLabel" Text="text" ID="id1" runat="server" ClientIDMode="Static">id1</asp:Label><br/>
<div id="div_id1" class="someclass" style="color:Red; display:none;">red</div>
<br/>
<asp:Label CssClass="clickLabel" Text="text" ID="id2" runat="server" ClientIDMode="Static">id2</asp:Label><br/>
<div id="div_id2" class="someclass" style="color:blue; display:none;">blue</div>
<br/>

<asp:Label CssClass="clickLabel" Text="text" ID="id3" runat="server" ClientIDMode="Static">id3</asp:Label><br/>
<div id="div_id3" class="someclass" style="color:green; display:none;">green</div>

<script type="text/javascript">
    $(document).ready(function () {
        $(".clickLabel").click(function () {
            var div_id = "#div_" + $(this).attr("id");
            $(".someclass").not(div_id).css("display", "none");
            $(div_id).css("display", "block");
        });
    });
</script>

if you use update panel in your page you must use this code to get reason:

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(AsyncPostback);
function AsyncPostback() { //insert your code here };
because your javascript code can not run after one postback.
Good luck!

发布评论

评论列表(0)

  1. 暂无评论