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

标签 javascript jquery html css

所以我有一个脚本可以将获取的数据加载到 anchor 内的跨度中。

链接:

<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(){
            });
            });
        });

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

现在,当您将鼠标悬停在它上面时,您还可以将鼠标放在跨度内,如果该链接下方有更多链接,这对用户来说确实不友好。

我试过:

链接:

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

最佳答案

对于 <span><a> 之后使用:

$(document).ready(function(){
            $('a[tdbid] + span').each(function(){

            $(this).load('data/fetch.php?id='+ $(this).attr('tdbid'), function(){
            });
            });
        });

“最近”返回最近的祖先。您在这里需要的是下一个 sibling

关于javascript - jQuery .load 在最近的跨度而不是元素本身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28267690/

相关文章:

javascript - 如何在 emberjs 中定义 "class attributes"?

Javascript/JQuery 动态 UL 问题

javascript - 在 jQuery 的嵌套列表中选择没有子项的列表项父项

javascript - 我如何使用 Google Maps geocoder.getLatLng() 并将其结果存储在数据库中?

html - 高度 :100% css is not working

javascript - 不允许 HTTP 405 方法,似乎是 CORS 问题

javascript - 从 State React.JS 渲染随机项

javascript - DropzoneJS : upload files and submit form with array of names

html - CSS 过渡不起作用?

html - 仅将 css 背景应用于表格单元格的文本内容