简体   繁体   English

显示/隐藏div标签javascript

[英]Show/Hide div tag javascript

I have a gridview column that's gets a large amount of text from the server. 我有一个gridview列,该列从服务器获取了大量文本。 So instead of showing all that text after the grid loads I want to show it only if a user clicks an Expand link then closes it with a collapse link. 因此,我不想显示所有文本,而是仅在用户单击“展开”链接然后通过折叠链接将其关闭时才显示该文本。 Here is what I have. 这就是我所拥有的。 Please note that I already know that I can put both javascript functions in one; 请注意,我已经知道我可以将两个javascript函数合而为一了。 I'm testing right now in two separate functions. 我正在两个独立的功能中进行测试。

<script type="text/javascript" language="javascript" >
function hidelink() {
    var col = $get('col');
    var exp = $get('exp');
    col.style.display = 'none';
    exp.style.display = '';

}
function showlink(){
    var col = $get('col');
    var exp = $get('exp');
   col.style.display = '';
   exp.style.display = 'none';
}

<asp:GridView ID="GridView2" Width="400px" runat="server" AutoGenerateColumns="False"   
AllowPaging ="True"
BackColor="White" BorderColor="#999999" 
BorderStyle="None" BorderWidth="1px" 
CellPadding="3" DataKeyNames="APPID" 
DataSourceID="SqlDataSource3" 
PagerSettings-Mode="NextPreviousFirstLast"                EnableSortingAndPagingCallbacks="True">

<PagerSettings Mode="NextPreviousFirstLast" />

 <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
 <Columns>
 <asp:BoundField DataField="stuff" HeaderText="Name" ReadOnly="True" 
 SortExpression="app" />
 <asp:BoundField DataField="Description" HeaderText="Short Descr" 
 ReadOnly="True" SortExpression="des" />
 <asp:TemplateField HeaderText="Long Descr" SortExpression="data">
 <EditItemTemplate>
 <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("data") %>'></asp:TextBox>
 </EditItemTemplate>
 <ItemTemplate>
 <div id="col">
 <asp:LinkButton ID="expand" runat="server" OnClientClick ="hidelink();return false;">Expand</asp:LinkButton>  
  </div>
  <div id="exp" style="display:none">
  <asp:LinkButton ID="collapse" runat="server" OnClientClick ="showlink();return false;">Collapse</asp:LinkButton>  
   </div>
  <asp:Panel ID="Panel1" runat="server" >
  <table>
    <tr>
        <td>                                                                     <%#Eval("LongDescription")%>
        </td>
     </tr>
  </table>

My issue is that only the first record does everything it should. 我的问题是只有第一条记录才能完成所有应做的工作。 (expand/collapse) but the other rows only expand and does not hide the expand link in the div tag. (展开/折叠),但其他行仅展开而不会在div标签中隐藏展开链接。 It is only finding the id of the first row because when the expand button is hit on any other row it changes the first row to show the collapse link. 它仅找到第一行的ID,因为当在任何其他行上单击扩展按钮时,它将更改第一行以显示折叠链接。 How can i fix this? 我怎样才能解决这个问题?

The problem is that because you have repeating elements, the ids of the DIVs are being reused. 问题是,因为您有重复的元素,所以DIV的ID被重用。 This is illegal in HTML. 这在HTML中是非法的。 The id property of each element must be unique. 每个元素的id属性必须是唯一的。 A better way to handle it is to pass in a reference to the current element to the handler and have it derive the element that it needs to operate on by traversing the DOM. 一种更好的处理方法是将对当前元素的引用传递给处理程序,并通过遍历DOM使其派生需要对其进行操作的元素。

<div>
   <asp:LinkButton ID="expand" runat="server" OnClientClick ="hidelink(this);return false;">Expand</asp:LinkButton>
</div>
<div style="display:none">
   <asp:LinkButton ID="collapse" runat="server" OnClientClick ="showlink(this);return false;">Collapse</asp:LinkButton>
</div>

Note: I'm using jQuery in these functions as it makes it easier to traverse the DOM. 注意:我在这些函数中使用jQuery,因为它使遍历DOM更容易。 You can do the same with your own DOM traversal functions and by setting the style properties if you like. 您可以使用自己的DOM遍历函数并通过设置样式属性(如果需要)执行相同的操作。

function hidelink(ctl) {
    var myDiv = $(ctl).closest('div');
    myDiv.hide();
    myDiv.next('div').show();
}

function showlink(ctl){
    var myDiv = $(ctl).closest('div');
    myDiv.hide();
    myDiv.prev('div').show();
}

You need to use unique IDs for each row. 您需要为每一行使用唯一的ID。 IDs can only apply to one element in the page, and your code is applying one ID to all the instances of this large column in the table. ID仅可应用于页面中的一个元素,并且您的代码将一个ID应用于表中此大列的所有实例。

Alternatively, you can just use the DOM methods to locate the right element to show/hide. 或者,您可以只使用DOM方法来定位要显示/隐藏的正确元素。 For example: 例如:

<div>
  <a href="#" onclick="showHideDesc(this); return false;">Expand</a>
  <table style="display: none;">
    <tr>
      <td><%#Eval("LongDescription")%></td>
    </tr>
  </table>
</div>

Then for your script: 然后为您的脚本:

function showHideDesc(link) {
    var table = link.parentNode.getElementsByTagName("TABLE")[0];
    if (table.style.display == "none") {
        table.style.display = "";
        link.innerHTML = "Collapse";
    }
    else {
        table.style.display = "none";
        link.innerHTML = "Expand";
    }
}

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

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