You are here: Home » All Categories » » 点击生成评论引用的jQuery代码

点击生成评论引用的jQuery代码

Aug 28th, 2010 10:18 | Leave a comment?(139) Go to comments

题外话:这几天一直在折腾Ajax评论提交,走了一个大圈还是回到了原点:willin的Ajax评论提交方式。不过算是了解了,可以自己升级修改了,精简了一些不必要的功能加快评论速度,再改改一些效果……麻烦大家继续测试ing

好,进入正题:这篇文章所说的内容其实对于熟悉jQuery的朋友来说非常简单,但因为有人要,就贴出来,顺便解决一下即将来临的文囧。

前提

1. 挂 jQuery 库,如果没有挂就把下面的js调用扔到 header.php 里面的 <?php wp_head() ?> 前面

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

2. 使用标准的评论结构,参考 WordPress 官方 http://codex.wordpress.org/Function_Reference/wp_list_comments 的 Examples 部分

准备

在functions.php找到主题的回调函数,找到类似下面的

<div class="reply">
	<?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
</div>

复制一份稍微修改一下,变成如下

<div class="reply">
	<?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
</div>
<div class="quote">
	<?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
</div>

注:你可能还需要修改 quote 显示样式css,最简单的就是在 .reply 上加上一个 .quote,自个折腾

jQuery代码:(把下面的添加到你的js文件或者直接加到footer.php的<?php wp_foot() ?>后面,用<script type="text/javascript"></script>引用)

2010.8.28 Edit)漏一句jQ代码,为复制的reply按钮文字换为“quote”

//评论引用 by zwwooooo
jQuery(".quote a").text("quote");//替换复制的reply按钮文字为quote
jQuery('.quote').click(function() {
	var atname = jQuery(this).parent().find('cite:first').text();//获得作者名称
	var quotecontent = jQuery(this).parent().find('.comment-content:first').html();//获得评论内容
	jQuery("#comment").attr("value","<blockquote><strong>" + atname + ": </strong>" + quotecontent + "</blockquote>\n").focus();//组合成引用
});

如果你要带@xxx的就如下:

//评论引用 by zwwooooo
jQuery(".quote a").text("quote");//替换复制的reply按钮文字为quote
jQuery('.quote').click(function() {
	var atid = '"#' + jQuery(this).parent().attr("id") + '"';//获取评论ID
	var atname = jQuery(this).parent().find('cite:first').text();//获得作者名称
	var quotecontent = jQuery(this).parent().find('.comment-content:first').html();//获得评论内容
	jQuery("#comment").attr("value","<a href=" + atid + ">@" + atname + " </a>\n" + "<blockquote><strong>" + atname + ": </strong>" + quotecontent + "</blockquote>\n").focus();//组合成引用
});

有些主题比较特殊,也就是说自定义了评论结构,那么就要根据自个主题修改 jQ 选择器了,不懂可以翻翻 jQuery 手册。

就这样,有兴趣的慢慢折腾。

声明: 除非注明,ZWWoOoOo文章均为原创,转载请以链接形式标明本文地址
本文地址: http://zww.me/archives/25284

Filed under

| Tags:

, ,

Related Posts

Most Popular

139 Comments.

⊕Leave a comment?
  1. QiQiBoY QiQiBoY Firefox 3.6.8Windows 7

    在你这里如果我想要引用一个人的评论就必须是对他进行回复,那这两条评论就在一块儿了,还引用干啥。。直接就能相互看到啊。。
    而且我点击引用a,再去点击引用B,结果评论框会清空对a的引用。。。 :arrow:

    31楼
    • zwwooooo zwwooooo Firefox 3.6.8Windows 7

      @QiQiBoY 第二层的讨论有时需要,但很少,清空是特意的,单个引用都基本没人用,2个的就基本不会有,玩下而已,一般用不到

  2. Bee君 Bee君 Firefox 3.6.8Windows 7

    这个可以在WordPress3的默认主题改造里试试

    32楼
  3. 立青 立青 Firefox 3.6.8Windows 7

    评论引用?没有用过,回去试试折腾,先备份

    33楼
  4. yetone yetone Google Chrome 7.0.503.0Windows XP

    引用,我觉得暂时还没必要用这个东东

    34楼
  5. 赵福安 赵福安 Google Chrome 7.0.503.0Windows XP

    :???: 天书...

    35楼
  6. 郑永 郑永 Firefox 3.0.8Windows XP

    很好,另外,很喜欢这个主题,呵呵:)

    36楼
  7. Firm Firm Google Chrome 6.0.490.0Windows XP

    有时会用得到,如果是读书类的博客这个功能应该会很喜欢

    37楼
  8. zwwooooo zwwooooo Google Chrome 5.0.375.127Windows 7

    @大智若鲁 下次扔spam列表

    38楼
  9. 如水 如水 Internet Explorer 8.0Windows XP

    博主也太专业了,深入折腾啊。

    39楼
  10. huangjun huangjun Internet Explorer 6.0Windows XP

    我刚折腾出一个牙博士博客,呵呵~

    40楼
  11. 风小 风小 Firefox 3.6.6Windows XP

    :smile: 很不错,我也要多学学jQuery了。。

    41楼
  12. 园子 园子 Firefox 3.6.8Windows XP

    没时间折腾这个了
    呵呵。

    42楼
  13. 有点蓝 有点蓝 TheWorld BrowserWindows XP

    会是什么效果呢? 测试一下。

    43楼
  14. 小邪 小邪 Google Chrome 6.0.496.0Windows XP

    唔,如果无限嵌套的时候顺序乱掉了,倒是必须的呢 ~

    44楼
  15. qiumu qiumu Firefox 4.0Windows 7

    :evil: 我怎么弄都不行,不知道为什么。

    46楼
    • zwwooooo zwwooooo Namoroka 3.6.17Windows 7

      @qiumu
      这个要懂点jQuery才行,或者你的主题用的评论结构跟默认主题一样。

  16. 主角 主角 Google Chrome 16.0.912.75Windows XP

    正在寻找完美的解决方法,能够即时显示GRAVATAR头像。

    47楼
  17. emric emric Google Chrome 18.0.1025.152Windows Server 2003
    <div id="comment-2" class="comment-body">
    <div class="comment-author"><img alt="" src="" class="avatar avatar-28 photo" ></div>
    <div class="comment-head">
    <span class="name">emric</span>
    <div class="date">四月 13, 2012 18:33:18 #1</div>
    </div>
    <div class="comment-entry">
    <div class="comment-text">
    <p>测试测试</p>
    </div>
    <div class="comment-reply"><a class="comment-reply-link" href="" onclick="">@</a></div>
    </div>
    </div>

    评论样式素这个, 我真的没辙了 TAT,木会JS

    48楼
    • zwwooooo zwwooooo Firefox 11.0Windows 7 x64 Edition

      @emric
      你这结构肯定不能直接使用文章中的代码,我文中好像说了,评论结构要跟 WP 官方的那种结构相同才能直接套用,不然就要自己根据自己的评论结构修改 jQuery 代码。

    • emric emric Google Chrome 18.0.1025.152Windows Server 2003

      @zwwooooo
      改了好几次吧,就素获取不到ID, 和名字,

    • zwwooooo zwwooooo Firefox 11.0Windows 7 x64 Edition

      @emric
      假定你在(这是根据你评论提供的结构复制的)

      <div class="comment-reply"><a class="comment-reply-link" href="" onclick="">@</a></div>

      后面复制了一份回复按钮为如下

      <div class="comment-reply quote"><a class="comment-reply-link" href="" onclick="">@</a></div>

      然后你试试修改过的jQ:

      jQuery(".quote a").text("quote");//替换复制的reply按钮文字为quote
      jQuery('.quote').click(function() {
      	var atid = '"#' + jQuery(this).parent().parent().attr("id") + '"';//获取评论ID
      	var atname = jQuery(this).parent().parent().find('.name:first').text();//获得作者名称
      	var quotecontent = jQuery(this).parent().find('.comment-text:first').html();//获得评论内容
      	jQuery("#comment").attr("value","<a href=" + atid + ">@" + atname + " </a>\n" + "<blockquote><strong>" + atname + ": </strong>" + quotecontent + "</blockquote>\n").focus();//组合成引用
      });
    • emric emric Google Chrome 18.0.1025.152Windows Server 2003

      @zwwooooo
      TAT, 成功了, ZWO 大叔素好淫,好淫,

    • zwwooooo zwwooooo Firefox 11.0Windows 7 x64 Edition

      @emric
      好淫难做,好淫吃亏。共享精神+刚好有时间罢了 :grin:

    • emric emric Google Chrome 18.0.1025.152Windows Server 2003

      @zwwooooo
      很猥琐的说肿么去掉IMG这些标签呢?unwrap()? :grin:

    • emric emric Google Chrome 18.0.1025.152Windows Server 2003

      @zwwooooo
      找到了 =v = 素text()

  18. SEV SEV Google Chrome 26.0.1410.64Windows 8 x64 Edition

    问题是二级后的评论引用,把@xx和链接给引用进去了,怎么办排除呢?

    49楼

Leave a Reply


Trackbacks and Pingbacks:

Welcome! o(∩_∩)o
X