2012-01-07 110 views
1

我正在嘗試使用Wordpress的「回覆」按鈕進行評論,並使其獲取您正在回覆的評論的用戶名,並將其發佈到評論textarea框中@USERNAME當一個鏈接被點擊時,獲取另一個類的內容

下面是我的Wordpress評論的輸出。第6行的存在,有一個類名url一個鏈接,鏈接文本是jasondavis

下越往下第15行存在的回覆鏈接,它具有類的comment-reply-link

當用戶點擊該回復鏈接,我想在這種情況下獲得用戶名jasondavis,所以我可以稍後將其插入到textarea框中。

我認爲這是可以使用jQuery來完成,假設有多個被點擊的回覆鏈接,所以當頁面上這些評論的,它必須足夠聰明,從.url得到文本,但只能從直接.url在該答覆按鈕/鏈接被點擊的特定評論的回覆按鈕之前。

如果您有任何建議或可以幫助我將不勝感激

<li class="comment byuser comment-author-jasondavis bypostauthor even depth-2" id="comment-6"> 
    <div class="c-grav"><img alt="" src="http://1.gravatar.com/" class="avatar avatar-60 photo" height="60" width="60"></div> 
    <div class="c-arrow"></div> 
    <div class="c-body"> 
     <div class="c-head"> 
      <a href="http://www.codedevelopr.com/" rel="external nofollow" class="url">jasondavis</a> 
      <span class="c-date">December 30, 2011 8:00pm</span> 
     </div> 
     <p>Comment body text is here......</p> 

     <span class="edit-comment"> 
      <a class="comment-edit-link" href="" title="Edit comment">Edit</a> 
      <a href="">Delete</a> 
      <a href="">Spam</a> 
      <a class="comment-reply-link" href="/codedevelopr/php-database-class/?replytocom=6#respond" onclick="return addComment.moveForm(&quot;comment-6&quot;, &quot;6&quot;, &quot;respond&quot;, &quot;5&quot;)">Reply</a> 
     </span> 
    </div><!--end c-body--> 
</li> 

回答

3

假設您的評論ulcomments的ID:

$("#comments").on('click', '.comment-reply-link', function(e) { 
    var name = $(this).parents('.comment').first().find('.url').text(); 
    alert('replying to ' + name); 
    e.preventDefault(); 
}); 

上的jsfiddle Try it

+0

這是真棒它讓我看到它是可行的,我將與它的一些玩,但它是在正確的軌道上,以便謝謝!我遇到的問題是我也有線索評論,所以如果評論有5個回覆,我點擊主評論上的回覆按鈕,它將顯示所有5條評論中的長名字,如果只有1條評論我也在回覆,然後顯示的只是1個名字,所以只需要找出扭曲的結果 – JasonDavis 2012-01-07 08:02:05

+0

@jasondavis:你應該可以在添加'.first()'或'.last()'之後'.find('。url')'停止解決這個問題。 – icktoofay 2012-01-07 22:45:13

1

嘗試this還的jsfiddle:

$(".comment-reply-link").click(function() { 
    username = $(this).parent().parent().children(".c-head").children(".url").html(); 
    alert(username); 
    return false; // to prevent further link processing by the browser. 
}); 

來獲取用戶名的值,你必須通過標籤的節點走。

+0

這很像icktoofay的例子,但沒有我在他的評論中發佈的問題,這似乎解決了這個問題,並顯示正確的名稱,謝謝! – JasonDavis 2012-01-07 08:04:18

+0

我編輯了一些代碼,加入:return false;以避免瀏覽器通過鏈接重定向頁面。 – Aryo 2012-01-07 08:10:53

1

另一種替代方案,只是工作有點不同,以。家長:

var name = $(this).closest('.comment').find('.url').text(); 
+0

這也適用,但在我發佈在@ icktoofay的答案上的情況中也有同樣的問題。我意識到我沒有發佈顯示嵌套評論回覆的代碼,所以這是我的錯 – JasonDavis 2012-01-07 08:07:01

相關問題