jQuery .load 在最近的跨度而不是元素本身

jQuery .load in closest span instead of element itself

本文关键字:元素 load 最近 jQuery      更新时间:2023-09-26

所以我有一个脚本,可以将提取的数据加载到锚点内的跨度中。

链接:

<a id="dbobject" tdbid="1">Weapon of mass destruction<span>fetch content goes here</span></a>

Jquery:

$(document).ready(function(){
            $('a[tdbid]').each(function(){
            $(this).load('data/fetch.php?id='+ $(this).attr('tdbid'), function(){
            });
            });
        });

内容正在完美地加载到跨度中,当您将鼠标悬停在链接上时,它会显示包含内容的跨度。但我宁愿将跨度放在链接之外,所以当您将鼠标从锚点上移开时,窗口会消失,

现在,当您将鼠标悬停在它上面时,您还可以将鼠标定位在范围内,如果该链接下方有更多链接,这确实不是用户友好的。

我尝试做:

链接:

<a id="dbobject" tdbid="1">Weapon of mass destruction</a><span>fetch content goes here</span>

Jquery:

$(document).ready(function(){
            $('a[tdbid]').each(function(){
            $(this).closest('span').load('data/fetch.php?id='+ $(this).attr('tdbid'), function(){
            });
            });
        });

这是行不通的。但是在这样做的时候,我想,如果我想在悬停时显示该跨度,那么我必须将跨度默认设置为隐藏。因此,没有链接的整个页面的跨度也将隐藏,这不是我打算做的。

那么,如何使悬停显示包含已获取内容的div呢?

对于<a>后的<span>,请使用:

$(document).ready(function(){
            $('a[tdbid] + span').each(function(){
            $(this).load('data/fetch.php?id='+ $(this).attr('tdbid'), function(){
            });
            });
        });

"最接近"返回最接近的祖先。您在这里需要的是下一个兄弟姐妹