简体   繁体   English

单击按钮显示div

[英]Display a div on button click

I have a div, whose display is set to none. 我有一个div,其显示设置为none。 On button click, I need to show this div. 单击按钮时,我需要显示此div。
I have wrote a JavaScript function to do so, and it works but on click of the button the div is shown and again it hides. 我已经编写了一个JavaScript函数来执行此操作,并且该函数可以工作,但是单击该按钮时,将显示div并再次将其隐藏。 What is to be done additionally so that is remains. 另外要做的是保留。

JavaScript is as follows: JavaScript如下:

function show_popup() {
    document.getElementById("Div1").style.display = 'block';
}

And div is as follows: 而div如下:

<div id="Div1" style="display:none">

The JavaScript function is called on button click as follows: 单击按钮时将调用JavaScript函数,如下所示:

<asp:Button ID="Button3" runat="server"  OnClientClick="javascript: show_popup()"  Text="Button" />

Please help me out. 请帮帮我。

If I interpret your question correctly: the div resets to be hidden on page refresh or navigation. 如果我正确解释了您的问题: div重置为在页面刷新或导航时隐藏。 This is to be expected, the display value should be handled on the server side too. 可以预期,显示值也应在服务器端处理。

As others have said, the problem is that when you click on the button, the page is posted to the server. 就像其他人所说的那样,问题在于,当您单击按钮时,页面将发布到服务器上。 If you do not want to do this, you should use a normal HTML button (with type="button" ), instead of an ASP one. 如果您不想这样做,则应使用普通的HTML按钮( type="button" ),而不要使用ASP type="button"

Your Javascript is working, the Problem seems to be the Button implemented with ASP. 您的Javascript正常工作,问题似乎出在ASP中。

The Problem could be the Serversided implementation of the Button, here is the HTML implementation. 问题可能是Button的服务器端实现,这是HTML实现。 Things are working there. 事情在那儿工作。

 function show_popup() { document.getElementById("Div1").style.display = 'block'; } 
 <div id="Div1" style="display:none">asdasdasdasdasdasd</div> <input type="button" onclick="show_popup()" value="Show"></input> 

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

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