简体   繁体   English

单击链接时如何在同一页面上显示框

[英]how to display box on same page when a link is clicked

I am making php based web application .I'd like to have a box displayed on the same page when a link is being clicked using java script.How can i achieve this?? 我正在制作基于php的Web应用程序。我想在使用Java脚本单击链接时在同一页面上显示一个框。如何实现? I have tried the below code 我已经尝试了以下代码

<script>
function a()
{
    document.getElementsById("a").style.visibility="visible";
}
</script>

<a style="position:absolute;left:84%;top:32%;font-size:13px " href="" onclick="a()">
    Forgot Password?
</a>

<div id="a" style="position:absolute;left:30%;top:10%;width:40%;height:40%;background-color:lightgray;visibility:hidden">
</div>

You needed to fix getElementsById isn't plural, should be getElementById 您需要修复getElementsById不是复数,应该是getElementById

To stop the page reloading on click, set href="javascript:a()" 要停止点击重新加载页面,请设置href="javascript:a()"

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">

<script>
function a()
{
    document.getElementById("a").style.visibility="visible";
}
function close_a()
{
    document.getElementById("a").style.visibility="hidden";
}
</script>

<a style="position:absolute;left:84%;top:32%;font-size:13px " href="javascript:a()">
    Forgot Password?
</a>

<div id="a" style="position:absolute;left:30%;top:10%;width:40%;height:40%;background-color:lightgray;visibility:hidden">
<a href="javascript:close_a()"><i class="fa fa-times-circle"></i> Close Me</a>
</div>

added function to close and font awesome 添加了关闭和字体效果的功能

The method of document is wrong. 文件方法错误。

It should be getElementById , you could use Google chrome DEV tools ( Ctrl + shift + i on windows, + option + i on Mac) to debug your code. 它应该是getElementById ,您可以使用Google chrome DEV工具(在Windows上为Ctrl + shift + i ,在Mac上为 + option + i )来调试代码。

Change your code to this and the box will be displayed: 将您的代码更改为此,将显示框:

<script>
function a()
{
    document.getElementById("a").style.display="block";
    return false;
}
</script>

<a style="position:absolute;left:84%;top:32%;font-size:13px " href="#" onclick="return a()">
    Forgot Password?
</a>

<div id="a" style="position:absolute;left:30%;top:10%;width:40%;height:40%;background-color:lightgray;display: none">
</div>

There was an error in your code. 您的代码中有错误。 getElementById is correct, and your <a> link refresh the page, correct method to avoid refresh is to change onclick="a();" getElementById是正确的,并且您的<a>链接刷新页面,避免刷新的正确方法是更改onclick="a();" to onclick="return a();" onclick="return a();" so that javascript look for return of function, and in function, we return false, therefore keep page same and avoid refresh. 因此javascript会寻找函式的返回,而在函式中,我们会传回false,因此请保持页面不变,并避免刷新。

Additionally you can add content to your box: 此外,您可以将内容添加到框中:

<script>
function a()
{

    document.getElementById("a").style.display="block";
    document.getElementById("a").innerHTML="<b>your other contents inside the box</b>";
    return false;
}
</script>

<a style="position:absolute;left:84%;top:32%;font-size:13px " href="#" onclick="return a();">
    Forgot Password?
</a>

<div id="a" style="position:absolute;left:30%;top:10%;width:40%;height:40%;background-color:lightgray;display: none">
</div>

暂无
暂无

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

相关问题 在html页面中单击时如何在对话框中显示相同的图像? - how to display the same image in dialog box when it is clicked in a html page? 单击链接时显示页面的隐藏内容 - Display hidden content of page when link is clicked 在反应中单击提交按钮时如何在同一页面上显示状态 - How to display the state on the same page when clicked Submit button in react 记录单击的链接,然后在框中将页面名称显示为历史记录 - Record a clicked link and then display the page name in a box as history 如何显示确认框<link to="###">被点击 - How to show a confirmation box when a <link to="###"> is clicked 在jQuery中单击链接时如何滑动页面 - How to slide page when link is clicked in Jquery 单击链接到同一页面的不同部分时,禁用URL更改 - disable url change when link to a different part of the same page is clicked 单击链接时如何在同一页面上显示另一个HTML文件的内容? - How to show another HTML file's contents on the same page when a link is clicked? 当单击另一个选项卡(链接)并且用户留在同一页面上时,如何防止 CSS 发生变化? - How to prevent CSS from changing when another tab(link) is clicked and the user is remaining on the same page? 如何存储/访问表(el-table)中的特定行数据并在单击该行中的链接时在对话框中显示(不是警报)? - How to store/access the specific row data in a table (el-table) and display that in an dialog box (not alert) when clicked on a link in that row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM