比方: A 留言了, B 用 @ 回复了 A, 所以 B 的回复可能是这样的:
@A
How much money do you have?
就是说, 当鼠标悬停在 @A 下面的时分, 就会将 A 的评论内容显示在一个悬浮区域中.
完成步骤
在这里咱们将以iNove主题为例进行解说。
1. 将以下代码保留为commenttips.js:
jQuery(document).ready( function(){ var id=/^#comment-/; var at=/^@/; jQuery('#thecomments li p a').each( function() { if(jQuery(this).attr('href').match(id)&& jQuery(this).text().match(at)) { jQuery(this).addClass('atreply'); } } ); jQuery('.atreply').hover( function() { jQuery(jQuery(this).attr('href')).clone().hide().insertAfter(jQuery(this).parents('li')).attr('id','').addClass('tip').fadeIn(200); }, function() { jQuery('.tip').fadeOut(400, function(){jQuery(this).remove();}); } ); jQuery('.atreply').mousemove( function(e) { jQuery('.tip').css({left:(e.clientX+18),top:(e.pageY+18)}) } ); } )
2. 将 commenttips.js 文件搁置到 inove/js 目录.
3. style.css 中追加款式代码如下:
#thecomments .tip { background:#FFF; border:1px solid #CCC; width:605px; padding:10px !important; padding:10px 10px 0; margin-top:0; position:absolute; z-index:3; } #thecomments .tip .act { display:none; } *+html #thecomments .tip { padding:10px 10px 0 !important; }
4. 在主题中增加代码调用 JavaScript. 关上 templates/end.php, 在 </body> 后面一行增加以下代码:
(假如你有其余插件或许本人曾经增加了 jQuery 的库, 那**行代码能够不用增加.)
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/commenttips.js"></script>
5. 好了, 刷新一下有 @ 回复的页面, 等页面加载完, 将鼠标悬停在 @ 回复上, 你会看到成果的.
为什么不能跨页显示?
由于其工作原理是, 当鼠标挪动到 @{username} 时在本页找到对应的评论, 并拔出到评论列表中, 以相对地位的形式显示进去. 假如评论不在本页, 找不到对象, 当然就没有前面的解决了.
如何跨页获取评论信息?
假如本页找不到对应的评论, 能够经过评论的 ID, 用 AJAX 将后盾查问到的评论信息前往页面. 当鼠标挪动到 @ 评论上时, 向用户悬浮显示 'Loading...' 提醒框, 假如操作胜利将找到的评论拔出评论列表的最初面, 并将该评论的内容置换到 'Loading...' 框.
也就是说, 被加载过的评论会不断保存在本页中, 当鼠标再次挪动到 @ 评论上不必从新加载.
上面咱们来看一下针对跨页评论的解决办法:
在以后页面如何经过 @{username} 找到对应评论?
1. 每个评论都会有一个 ID, 构造如: comment-{commentId}, 这本是为了不便经过锚点找到评论, 同时也成为实现 @ 评论提醒的必要条件.
2. 每个 @{username} 其实就是指向评论的锚点, 天然能够获得评论 ID.
所以其实很简略, 假如评论 ID 是 _commentId, 那么在 JS 能够经过以下代码找到对应的评论.
document.getElementById(_commentId);
假如可以找到指标评论, 则创立一个暗藏的暂时评论, 并以指标评论作为其内容, 在 @{username} 附件将它显示进去; 假如没找到指标评论, 则经过 ID 到后盾查找对应的评论, 进行跨页解决.
如何跨页加载评论?
跨页的本质是静态加载评论, 将获取的评论追加到评论列表最初, 让评论能够在本页中找到, 不同的只是这些评论经过 CSS 加工并不会显示进去.
能够参考下图. 假如评论不在本页, 会走白色门路, 在评论被退出以后页面之后, 会有一个举措, 将提醒框的 Loading 信息交换为评论内容. 当用户在此将鼠标悬停在这个 @{username} 时, 评论已在以后页面, 所以不需再次加载, 而是走绿色门路, 间接将评论提醒框调出.
注: 图中蓝色局部是后盾解决, 黄色局部是整个加载进程的重点.
在后盾中怎么获取评论并对其格局化?
这里能够本人写个办法对评论信息进行格局化, 也能够经过评论的回调办法 (WordPress 2.7 或以上版本能够定义评论的回调办法) 来获取格局化的 HTML.
$comment = get_comment($_GET['id']); custom_comments($comment, null,null);
注: custom_comments 是我的回调函数的办法名.
JavaScript 代码
基于 jQuery 的 JS 代码, 假如不应用或许应用其余 JS frame, 请依据解决思绪自行革新. 倡议将代码搁置于评论列表下方.
var id=/^#comment-/; var at=/^@/; jQuery('#thecomments li p a').each(function() { if(jQuery(this).attr('href').match(id)&& jQuery(this).text().match(at)) { jQuery(this).addClass('atreply'); } }); jQuery('.atreply').hover(function() { var target = this; var _commentId = jQuery(this).attr('href'); if(jQuery(_commentId).is('.comment')) { jQuery('<li class="comment tip"></li>').hide().html(jQuery(_commentId).html()).appendTo(jQuery('#thecomments')); jQuery('#thecomments .tip').css({ left:jQuery().cumulativeOffset(this)[0] + jQuery(this).width() + 10, top:jQuery().cumulativeOffset(this)[1] - 22 }).fadeIn(); } else { var id = _commentId.slice(9); jQuery.ajax({ type: 'GET' ,url: '?action=load_comment&id=' + id ,cache: false ,dataType: 'html' ,contentType: 'application/json; charset=utf-8' ,beforeSend: function(){ jQuery('<li class="comment tip"></li>').hide().html('<p class="ajax-loader msg">Loading...</p>').appendTo(jQuery('#thecomments')); jQuery('#thecomments .tip').css({ left:jQuery().cumulativeOffset(target)[0] + jQuery(target).width() + 10, top:jQuery().cumulativeOffset(target)[1] - 22 }).fadeIn(); } ,success: function(data){ var addedComment = jQuery(data + '</li>'); addedComment.hide().appendTo(jQuery('#thecomments')); jQuery('#thecomments .tip').html(addedComment.html()); } ,error: function(){ jQuery('#thecomments .tip').html('<p class="msg">Oops, failed to load data.</p>'); } }); } }, function() { jQuery('#thecomments .tip').fadeOut(400, function(){ jQuery(this).remove(); }); });
PHP 代码
这段代码来自PhilNa2 主题, 倡议将代码追加到 function.php.
function load_comment(){ if($_GET['action'] =='load_comment' && $_GET['id'] != ''){ $comment = get_comment($_GET['id']); if(!$comment) { fail(printf('Whoops! Can\'t find the comment with id %1$s', $_GET['id'])); } custom_comments($comment, null,null); die(); } } add_action('init', 'load_comment');
以上就是安达网络工作室关于《利用jQuery实现WordPress中@的ID悬浮显示评论内容》的一些看法。更多内容请查看本栏目更多内容!
导航菜单 导航菜单早已 "深化民意", 在博客上的使用日益重要且多样. 从本文开端, 我将展开几个对于 WordPre...
渣滓评论曾经不是一个新颖的货色了,置信只需是日独立IP超越2000的网站,只需开放评论的话,每天都会收到几...
本文实例总结了wordpress随机调用显示文章的办法。分享给大家供大家参考。详细办法如下: 在wordpress中要随...
本文实例讲述了WordPress修正评论默许头像的办法。分享给大家供大家参考。详细剖析如下: 咱们晓得wordpres...
WordPress不管是在顺序构造还是模板标签的定义上都十分重视搜寻引擎优化的概念,所以在Google等知名的搜寻引...
本文实例讲述了WordPress完成网站投稿者也能够上传图片的办法。分享给大家供大家参考。详细剖析如下: Word...