所以我有一个脚本可以将获取的数据加载到 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/