当用户将鼠标悬停在特定的文本字符串上时,我正在使用 jquery hovercard 插件从使用 ajax 的文本文件中提取文本 - 这一切都很好。
我已经添加了在页面加载时搜索内容并将label
类添加到任何匹配名称的功能 - 这在定义名称时非常有用。
在我上一个问题的帮助下;(非常感谢@Alex Klock),我已经得到它,因此只有一个 div 的 text/html 从悬停时的文本文件中拉入 - 在定义名称时效果很好。
我现在需要做的就是将它们拉到一起并使其动态化,以便在页面加载时将所有名称从文本文件中提取出来,将正确的label
类添加到相关名称中,然后添加正确/对应的文本/html 在悬停时显示。
任何帮助将不胜感激:)
//HTML代码
<div id="content">
<p>jQuery by John Resig is a cross-browser JS library designed to simplify the client-side scripting of HTML. It was released in January of 2006 at BarCamp NYC. Used by over 46% of the 10,000 most visited websites, it's the most popular JavaScript library in use today Tim Berners-Lee.</p>
<p>jQuery is free, Tim Berners-Lee open source software, dual-licensed under the MIT License and GNU General Public License, Tim Berners-Lee Version 2.[4] jQuery's syntax is designed to make it easier to navigate a document, select DOM elements, create animations, handle events, and John Resig develop Ajax applications.</p>
</div>
//jquery代码
//based on Highlight words in text with jQuery by (http://www.gotoquiz.com/web-coding/programming/javascript/highlight-words-in-text-with-jquery/)
jQuery.fn.addhover = function (str, className) {
var regex = new RegExp(str, "gi");
return this.each(function () {
$(this).contents().filter(function() {
return this.nodeType == 3 && regex.test(this.nodeValue);
}).replaceWith(function() {
return (this.nodeValue || "").replace(regex, function(match) {
return "<label class=\"" + className + "\" style=\"color:#932121;\">" + match + "</label>";
});
});
});
};
$(document).ready(function () {
var HTML_FILE_URL = 'helloworld.txt';
$.get(HTML_FILE_URL, function(data) {
var fileDom = $(data);
fileDom.filter('.contact').each(function() {
var savename = $(this).closest(".contact").attr("id");
});
});
$("#content *").addhover("John Resig", "demo-ajax");
var hoverHTMLDemoAjax = '<div class="demo-cb-tweets"></div>';
$(".demo-ajax").hovercard({
detailsHTML: hoverHTMLDemoAjax,
width: 350,
delay: 500,
cardImgSrc: 'http://ejohn.org/files/short.sm.jpg',
onHoverIn: function () {
$.ajax({
url : "helloworld.txt",
type: 'GET',
dataType: "text",
beforeSend: function () {
$(".demo-cb-tweets").prepend('<p class="loading-text">Loading latest tweets...</p>');
},
success: function (data) {
var people = $(data),
john = people.filter('#John_Resig');
$(".demo-cb-tweets").empty().append(john);
},
complete: function () {
$('.loading-text').remove();
}
});
}
});
});
//Helloworld.txt文件内容
<div class="contact" id="John_Resig">
<p><strong>John_Resig Testing testing testing!</strong></p>
<p>And on another line of text : )</p>
</div>
<div class="contact" id="Tim_Berners_Lee">
<p><strong>Tim_Berners_Lee Testing testing testing!</strong></p>
<p>And on another line of text : )</p>
</div>