用按钮显示一些文本 - JavaScript

时间:2015-09-09 13:51:42

标签: javascript c# html

我在HTML中有一些隐藏文字,当我点击一个按钮时我想要显示但同时我希望该按钮在C#中生成代码

我使用此代码创建JavaScript函数

function toggle() {

var ele = document.getElementById("dpt");
var text = document.getElementById("Button1");
if(ele.style.display == "block") {
        ele.style.display = "none";
    text.innerHTML = "show";
}
else {
    ele.style.display = "block";
    text.innerHTML = "hide";

这是html中的文字和按钮

        <td id="dpt" style="display: none"> <b>Dias previstos de trabalho:</b> <asp:Label ID="Lbl_DiasTrab" runat="server"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp; </td>
    <td id= "md" style="display: none"> <b>Media Diária:</b> <asp:Label ID="Lbl_Media" runat="server"></asp:Label></td>
    <td id= "nrmed" style="display: none"> <b>Número de Médicos:</b> <asp:Label ID="LblNrMedicos" runat="server"></asp:Label></td>
    <td id= "indcob" style="display: none"> <b>Índice de Cobertura:</b> <asp:Label ID="LblIndCob" runat="server"></asp:Label></td>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Ver"  Width="74px" Height="30px" />

与此同时,我做到了,但当我点击按钮时,它会刷新页面

我在html上使用了这段代码

<asp:Button ID="Button1" runat="server" onclick="Button1_Click"  OnClientClick="toggle()" Text="Ver"  Width="74px" Height="30px" />

1 个答案:

答案 0 :(得分:0)

@Press是对的,您的ID将在服务器端被修改,然后才会发送给客户端。相反,将this传递给toggle,它将是对您的按钮的引用:

<强> JavaScript的:

function toggle(text) {

    var ele = document.getElementById("dpt");
    if(ele.style.display == "block") {
        ele.style.display = "none";
        text.innerHTML = "show";
    }
    else {
        ele.style.display = "block";
        text.innerHTML = "hide";
    }
}

<强> HTML:

<td id="dpt" style="display: none"> <b>Dias previstos de trabalho:</b> <asp:Label ID="Lbl_DiasTrab" runat="server"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp; </td>
<td id= "md" style="display: none"> <b>Media Diária:</b> <asp:Label ID="Lbl_Media" runat="server"></asp:Label></td>
<td id= "nrmed" style="display: none"> <b>Número de Médicos:</b> <asp:Label ID="LblNrMedicos" runat="server"></asp:Label></td>
<td id= "indcob" style="display: none"> <b>Índice de Cobertura:</b> <asp:Label ID="LblIndCob" runat="server"></asp:Label></td>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"  OnClientClick="toggle(this)" Text="Ver"  Width="74px" Height="30px" />