jquery - 自动将链接标题附加到 div 标签内

标签 jquery tooltip

我想使用 jquery 创建一个 CSS 工具提示。我的 html 代码是

<a class="tooltip" href="#" title="My ttitle">Link1</a>

它应该像悬停时一样

<a class="tooltip" href="#" title="My ttitle"><div class="classic">My title</div>Link1</a>

如何使用 jQuery 执行此操作?

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    //prepend span tag
    var title = $("this").attr("title");
    $(".tooltip").prepend("<div class='classic'>".$(this).attr("title")."</div>");  

});

最佳答案

在 JavaScript 中连接字符串时,您应该使用 + 而不是 .;

$(".tooltip").prepend("<div class='classic'>" + $(this).attr("title") + "</div>");  

另请注意,您不应将 this 对象用引号括起来,并且代码中的 this 引用 document 对象而不是您的 span 元素。

关于jquery - 自动将链接标题附加到 div 标签内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12675243/

相关文章:

javascript - 仅按年、月和日比较日期?

javascript - 检查 JSON 文件内数组中的值

r - 在 ggvis 图中隐藏工具提示初始化为 ="click"

javascript - 如何将工具提示图像添加到多个按钮

Jquery 模态工具提示插件

javascript - JS : Relative positioning of tooltip on right of icon

javascript - 使用 js.color 的自定义选择不起作用

javascript - jquery如何从文件夹中读取文件?

javascript - 数组值需要 Text

javascript - 悬停在使用 d3.js 创建的条形图条上后触发 2 个鼠标悬停事件?