亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

利用jQuery實(shí)現(xiàn)WordPress中@的ID懸浮顯示評論內(nèi)容

 更新時(shí)間:2015年12月11日 15:27:56   作者:吳釗  
這篇文章主要介紹了使用JavaScript實(shí)現(xiàn)WordPress中ID懸浮顯示評論的功能,就是在樓中樓式的評論中顯示被評論的主體內(nèi)容,需要的朋友可以參考下

比如: A 留言了, B 用 @ 回復(fù)了 A, 所以 B 的回復(fù)可能是這樣的:

@A
How much money do you have?

就是說, 當(dāng)鼠標(biāo)懸停在 @A 上面的時(shí)候, 就會將 A 的評論內(nèi)容顯示在一個(gè)懸浮區(qū)域中.

20151211152525545.png (480×200)

實(shí)現(xiàn)步驟
在這里我們將以iNove主題為例進(jìn)行講解。
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. 在主題中添加代碼調(diào)用 JavaScript. 打開 templates/end.php, 在 </body> 前面一行添加以下代碼:
(如果你有其他插件或者自己已經(jīng)添加了 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. 好了, 刷新一下有 @ 回復(fù)的頁面, 等頁面加載完, 將鼠標(biāo)懸停在 @ 回復(fù)上, 你會看到效果的.

為什么不能跨頁顯示?
因?yàn)槠涔ぷ髟硎? 當(dāng)鼠標(biāo)移動到 @{username} 時(shí)在本頁找到對應(yīng)的評論, 并插入到評論列表中, 以絕對位置的方式顯示出來. 如果評論不在本頁, 找不到對象, 當(dāng)然就沒有后面的處理了.

如何跨頁獲取評論信息?
如果本頁找不到對應(yīng)的評論, 可以通過評論的 ID, 用 AJAX 將后臺查詢到的評論信息返回頁面. 當(dāng)鼠標(biāo)移動到 @ 評論上時(shí), 向用戶懸浮顯示 'Loading...' 提示框, 如果操作成功將找到的評論插入評論列表的最后面, 并將該評論的內(nèi)容置換到 'Loading...' 框.
也就是說, 被加載過的評論會一直保留在本頁中, 當(dāng)鼠標(biāo)再次移動到 @ 評論上不用重新加載.
下面我們來看一下針對跨頁評論的處理方法:

在當(dāng)前頁面如何通過 @{username} 找到對應(yīng)評論?

1. 每個(gè)評論都會有一個(gè) ID, 結(jié)構(gòu)如: comment-{commentId}, 這本是為了方便通過錨點(diǎn)找到評論, 同時(shí)也成為完成 @ 評論提示的必要條件.
2. 每個(gè) @{username} 其實(shí)就是指向評論的錨點(diǎn), 自然可以取得評論 ID.

所以其實(shí)很簡單, 如果評論 ID 是 _commentId, 那么在 JS 可以通過以下代碼找到對應(yīng)的評論.

document.getElementById(_commentId);

如果能夠找到目標(biāo)評論, 則創(chuàng)建一個(gè)隱藏的臨時(shí)評論, 并以目標(biāo)評論作為其內(nèi)容, 在 @{username} 附件將它顯示出來; 如果沒找到目標(biāo)評論, 則通過 ID 到后臺查找對應(yīng)的評論, 進(jìn)行跨頁處理.

如何跨頁加載評論?

跨頁的實(shí)質(zhì)是動態(tài)加載評論, 將獲取的評論追加到評論列表最后, 讓評論可以在本頁中找到, 不同的只是這些評論通過 CSS 加工并不會顯示出來.

可以參考下圖. 如果評論不在本頁, 會走紅色路徑, 在評論被加入當(dāng)前頁面之后, 會有一個(gè)動作, 將提示框的 Loading 信息替換為評論內(nèi)容. 當(dāng)用戶在此將鼠標(biāo)懸停在這個(gè) @{username} 時(shí), 評論已在當(dāng)前頁面, 所以不需再次加載, 而是走綠色路徑, 直接將評論提示框調(diào)出.

20151211152628341.png (509×666)

注: 圖中藍(lán)色部分是后臺處理, 黃色部分是整個(gè)加載過程的重點(diǎn).

在后臺中怎樣獲取評論并對其格式化?

這里可以自己寫個(gè)方法對評論信息進(jìn)行格式化, 也可以通過評論的回調(diào)方法 (WordPress 2.7 或以上版本可以定義評論的回調(diào)方法) 來獲取格式化的 HTML.

$comment = get_comment($_GET['id']);
custom_comments($comment, null,null);

注: custom_comments 是我的回調(diào)函數(shù)的方法名.

JavaScript 代碼

基于 jQuery 的 JS 代碼, 如果不使用或者使用其他 JS frame, 請根據(jù)處理思路自行改造. 建議將代碼放置于評論列表下方.

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');

相關(guān)文章

最新評論