javascript - 使用javascript制作一种弹出窗口

标签 javascript html css

我有一个 HTML 页面,当点击一个链接时,我试图让一个弹出元素(只是一个出现在链接上的 div 框)出现在被点击的链接上方。我使用 javascript 来执行此操作,但我的问题是弹出元素定位在链接下方而它应该位于链接上方

你知道我做错了什么吗?我该如何解决?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/homepage.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>

    <style type="text/css" media="all">
        <!--

        html, body, div, form, fieldset, legend, label, img {  margin: 0;  padding: 0;  }  table {  border-collapse: collapse;  border-spacing: 0; }  th, td {  text-align: center;  }  h1, h2, h3, h4, h5, h6, th, td, caption { font-weight:normal; }  img { border: 0; } 

        body { padding: 20%; background-color: green; }

        .container { background-color: white; }
        .newEle { width: 100px; height: 100px; background-color: red; }

        -->
    </style>
    <script type="text/javascript">

        function getOffset( el ) 
        {     
            var _x = 0;
            var _y = 0;

            while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) )
            {
                _x += el.offsetLeft - el.scrollLeft;
                _y += el.offsetTop - el.scrollTop;
                el = el.parentNode;
            }     

            return { top: _y, left: _x }; 
        }  

        function onClick( n, ele )
        {
            // Should display a popup box just above the HTML element called "ele"
            // but what actually happens is that the box is displayed below the element
            // called "ele"

            var infoBox = document.createElement("div");

            infoBox.style.zIndex          = "5";
            //infoBox.offsetRight           = ele.offsetRight;
            //infoBox.offsetBottom          = parseInt(ele.offsetBottom, 10) - 200 + "px";
            infoBox.style.x                     = getOffset( ele ).left + "px";
            infoBox.style.y                     = getOffset( ele ).top  - 200 + "px";
            infoBox.style.width           = "200px";
            infoBox.style.height          = "200px";
            infoBox.style.backgroundColor = "blue";
            infoBox.innerHTML             = "Hello";

            document.body.appendChild( infoBox );
        }

    </script>
</head>

<body>

    <div class="container">
        <a class="newEle" onclick="onClick(1,this)">Create New Element</a>
    </div>

</body>
</html>

最佳答案

将此添加到您的 CSS。

.container, .newEle {display: block; float: left;}

然后绝对定位你的元素。

关于javascript - 使用javascript制作一种弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5507809/

相关文章:

php - 多次循环 jQuery .post 函数

javascript - waypoint.js 在第二部分不起作用

javascript - # url 部分隐藏和显示

javascript - 如何使用私钥和密码从地址发送以太币?

html - 在 anchor 标记中使用 bg-image 居中跨度

php - URL 重写破坏了到 css 的链接

javascript - 检查输入类型文本和单选

html - Bootstrap 4 横向显示属性不起作用?

css - 为 CSS3 多列添加一个最小高度

html - 不同列中第一个 div 的高度相同(bootstrap 4)