简体   繁体   English

单击链接后,获取另一个类的内容

[英]When a link is clicked, grab the contents of another class

I am trying to take the Wordpress "reply" button for comments and making it grab the username of the comment you are replying to and post it into the comment textarea box like @USERNAME 我正在尝试使用Wordpress的“回复”按钮进行评论,并使其抓住您要回复的评论的用户名,并将其发布到评论文本区域框中,例如@USERNAME

Below is the output from my Wordpress comment. 以下是我的Wordpress评论的输出。 On line 6 there is a link that has a Class name url and the links text is jasondavis 在第6行上,有一个具有类名称url的链接,并且链接文本为jasondavis

Down farther down on line 15 there is the Reply link, it has a class of comment-reply-link 在第15行的更下方有一个Reply链接,它具有一类comment-reply-link

When a user clicks on that reply link, I am trying to get the Username in this case jasondavis so I can later insert it into the textarea box. 当用户单击该回复链接时,在这种情况下,我试图获取用户名jasondavis以便以后将其插入到textarea框中。

I think this can be done with jQuery, assume there are multiple of these comments on the page so when the Reply link is clicked, it has to be smart enough to get the text from .url but only from the .url that directly precedes the reply button for that particular comment that the reply button/link was clicked from. 我认为可以使用jQuery完成此操作,假设页面上有多个这些注释,因此当单击Reply链接时,它必须足够聪明才能从.url获取文本,但只能从直接位于.url之前的.url中获取文本。单击了回复按钮/链接的特定评论的回复按钮。

If you have any tips or can help I would appreciate it 如果您有任何提示或可以帮助,我将不胜感激

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

Assuming your comment ul has an ID of comments : 假设您的评论ul具有comments ID:

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

Try it on JSFiddle. 试试吧上的jsfiddle。

Try this also on JSFiddle: 也可以在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.
});

To get value of the username, you have to walk through the tag nodes. 要获得用户名的值,您必须遍历标签节点。

另一个替代方法与.parents有点不同:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM