繁体   English   中英

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

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

我正在制作基于php的Web应用程序。我想在使用Java脚本单击链接时在同一页面上显示一个框。如何实现? 我已经尝试了以下代码

<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>

您需要修复getElementsById不是复数,应该是getElementById

要停止点击重新加载页面,请设置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>

添加了关闭和字体效果的功能

文件方法错误。

它应该是getElementById ,您可以使用Google chrome DEV工具(在Windows上为Ctrl + shift + i ,在Mac上为 + option + i )来调试代码。

将您的代码更改为此,将显示框:

<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>

您的代码中有错误。 getElementById是正确的,并且您的<a>链接刷新页面,避免刷新的正确方法是更改onclick="a();" onclick="return a();" 因此javascript会寻找函式的返回,而在函式中,我们会传回false,因此请保持页面不变,并避免刷新。

此外,您可以将内容添加到框中:

<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.

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