Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于表中单击元素的Div定位_Javascript_Jquery_Html - Fatal编程技术网

Javascript 基于表中单击元素的Div定位

Javascript 基于表中单击元素的Div定位,javascript,jquery,html,Javascript,Jquery,Html,我想知道是否可以根据单击表中的哪个元素来定位div。我的代码中有一个图像网格,如果单击了一个图像,我希望在图像旁边弹出一个speechballoon,为用户提供处理图像的选项 我现在有这个(尽管有更多的图片): 既然您使用的是jQuery,我建议您使用offset函数() 您需要使用CSS将“编辑”的位置设置为“绝对”: #edit{ position: absolute; } Javascript: $(".image").click(function(){ var

我想知道是否可以根据单击表中的哪个元素来定位
div
。我的代码中有一个图像网格,如果单击了一个图像,我希望在图像旁边弹出一个speechballoon,为用户提供处理图像的选项

我现在有这个(尽管有更多的图片):


既然您使用的是jQuery,我建议您使用
offset
函数()

您需要使用CSS将“编辑”的位置设置为“绝对”:

#edit{
        position: absolute;
}
Javascript:

$(".image").click(function(){
    var id = $(this).attr('id');
    $("#edit").fadeIn();
};
$(".image").click(function(){
    var id = $(this).attr('id');
    var offset = $(this).offset(); //get the clicked element's position
    offset.top += $(this).height(); // add the height, because we want the bubble
                                    // to be directly below
    $("#edit").offset(offset); //set 'edit's offset
    $("#edit").fadeIn();
});

看看小提琴:

太棒了!这就是方法。你到底在哪里发现了困难?问题是,我如何将悬停在正确图像上的speechballoon放在哪里。现在它只是在角落里徘徊。我不知道这是否与你建议的代码或我的代码有关,但它一直漂浮在桌面上。无论我点击哪个图像。(我的表格中有三个图像排成一行)。。隐马尔可夫模型。。我为一个你可能没有的CSS做了一个编辑,但无论如何它应该是有效的。你查过小提琴了吗?是的,我查过了。问题是我的代码是这样的:
  • $(".image").click(function(){
        var id = $(this).attr('id');
        var offset = $(this).offset(); //get the clicked element's position
        offset.top += $(this).height(); // add the height, because we want the bubble
                                        // to be directly below
        $("#edit").offset(offset); //set 'edit's offset
        $("#edit").fadeIn();
    });