jquery - 单击链接并调整位置时显示弹出窗口

标签 jquery html

我正在尝试在我的页面上的链接旁边创建一个小弹出窗口。我需要它是动态的,因为生成的链接数量是随机的。我希望该框每次都显示在链接旁边,因此这需要将位置调整到新坐标,我的问题是如何根据单击的链接以编程方式确定将框移动到哪里?我不确定如何解决这个问题,正在寻找一些建议。

HTML

<div style="display: none; border: 1px solid black; height: 150px; width: 250px; 
       padding: 5px; position: absolute; left: 100px; top: 100px; 
       background-color: silver;" id="messageBox">
    <textarea style="width: 225px; height: 115px;"></textarea>
    <button id="save" style="float: right;">Save</button>
</div>

<div class="productLine">
    <a href="#">Link #1</a><br /><br />
    <a href="#">Link #2</a><br /><br />
</div>
<br />
<div class="productLine">
    <a href="#">Link #3</a><br /><br />
    <a href="#">Link #4</a><br /><br />
</div>

jQuery:

$('.productLine a').click(function() {
    $('#messageBox').toggle();
});

最佳答案

您可以尝试这个 - (您可以在左侧和顶部添加 a 的宽度和高度,以在适当的位置显示框)

$('.productLine a').click(function () {
    var $this = $(this);
    $('#messageBox').css({
        left: $this.position().left + $this.width(),
        top: $this.position().top,
    }).toggle();
});

演示 -----> http://jsfiddle.net/esEP8/2/

关于jquery - 单击链接并调整位置时显示弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17216573/

相关文章:

javascript - 为什么选择 DOM 元素时使用常量变量?

html - 自制的 Shiny table

html - 如何使div在行中居中

javascript - bootstrap-select:将选项添加到多个选择框后,它无法正确刷新

javascript - 输入文本更改期间如何激活功能?

javascript - 问题获取菜单滑动

javascript - 点击()不工作

html - 我如何将边框设置为所需的长度?

html - 隐藏文本区

javascript - Jquery 提交功能无法与 console.log 结合使用