欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

@A
How much money do you have?

就是說(shuō), 當(dāng)鼠標(biāo)懸停在 @A 上面的時(shí)候, 就會(huì)將 A 的評(píng)論內(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. 打開(kāi) templates/end.php, 在 </body> 前面一行添加以下代碼:
(如果你有其他插件或者自己已經(jīng)添加了 jQuery 的庫(kù), 那第一行代碼可以不必添加.)

<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ù)的頁(yè)面, 等頁(yè)面加載完, 將鼠標(biāo)懸停在 @ 回復(fù)上, 你會(huì)看到效果的.

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

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

在當(dāng)前頁(yè)面如何通過(guò) @{username} 找到對(duì)應(yīng)評(píng)論?

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

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

document.getElementById(_commentId);

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

如何跨頁(yè)加載評(píng)論?

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

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

20151211152628341.png (509×666)

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

在后臺(tái)中怎樣獲取評(píng)論并對(duì)其格式化?

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

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

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

JavaScript 代碼

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

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 代碼

這段代碼來(lái)自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)文章

最新評(píng)論