从元素中选择html内容

时间:2017-05-17 22:14:22

标签: javascript jquery html

我想选择此元素的内容

<div class="entry-content">
    comment.body +
</div>

我想在下面调用的函数中获取该注释。更准确地说是在

var body = parent.find('entry-content');

如何选择某个div的内容?我尝试了val()contents(),但都没有。

有什么建议吗?

 $("body").on("click", ".show-textarea", function(){

    var answerid = $(this).data('answer');
    var type = $(this).data('type');
    var questionid = $(this).data('question');
    var $commentForm = $(this).parent().find('.comment-form').first();
    console.log(answerid);
    $.getJSON("/controller/api/comments/comment.php", {
        answerid : answerid,
        questionid : questionid,
        type : type
    }, function (data) {
        $commentForm.find('article').remove();
        console.log("AID " + answerid);
        console.log("Data" + data.length);
        $.each(data, function(i, comment) {
            console.log("Comment:" + comment);
            $commentForm.append('<article class="tweet-data">' +
                '<div class="comment-items">' +
                '<div class="qa-c-list-item  hentry comment" id="c3574">' +
                '<div class="asker-avatar">' +
                '<a>' +
                '<img width="40" height="40" src="' +
                comment.user_photo +
                '"></a>' +
                '</div>' +
                '<div class="qa-c-wrap">' +
                '<div class="post-button">' +
                '<button name="" onclick="" class="btn icon-flag" title="Flag this comment as spam or inappropriate" type="submit">flag</button>' +
                '<button name="" class="btn icon-answers" title="Reply to this comment" type="submit">reply</button>' +
                '</div>' +
                '<span class="qa-c-item-meta">' +
                'commented' +
                ' 1 day' +
                ' ago' +
                ' by ' +
                '<a style="display: inline" href="" class="qa-user-link url nickname">' +
                comment.username +
                '</a> ' +
                '<span class="qa-c-item-who-points"> ' +
                '<span class="qa-c-item-who-points-pad">(</span><span class="qa-c-item-who-points-data">140</span><span class="qa-c-item-who-points-pad"> points)</span> ' +
                '</span> ' +
                '</span> ' +
                '</span> ' +
                '<div class="qa-c-item-content" style="color: #2d2727; font-size: 13px"> ' +
                '<a name="3574"></a>' +
                '<div class="entry-content">' +
                comment.body +
                '</div> ' +
                '</div> ' +
                '<div class="comment-edit-form">' +
                '<form method="post" action="/controller/actions/comments/edit_comment.php">' +
                '<button class = "edit-comment btn {if !$isMine}hidden{/if}" type="button">Edit</button>' +
                '</form>' +
                '</div> ' +
                '</div> <!-- END qa-c-item --> ' +
                '</div>' +
                '</div></article>');
        });
    });

    $commentForm.show();
});

$("body").on("click", ".textarea-ok, .textarea-cancel", function(){
    commentsFetched = false;
    $('.comment-form').hide();

});

$("body").on("click", ".edit-comment", function(){
    console.log("Hello");
    var parent = $("body").find('article').last();
    var body = parent.find('entry-content');
    console.log(body);
    var parent_parent = $("body").find('article').parent().last();
    parent.remove();
    console.log(parent);
    var button = $(this),
        commentField = $('<div class="comment-form">' +
            '<form method="post" action="/controller/actions/comments/edit_comment.php">' +
            '<textarea name="comment" rows="4" cols="40" class="qa-form-tall-text">' +
            body +
            '</textarea>' +
            '<input type="hidden" name="answerid" value="" />' +
            '<input type="hidden" name="questionid" value="" />' +
            '<button type="submit" class="textarea-ok">Edit Comment</button>' +
            '</form>' +
            '</div>'); // create a textarea element

    commentField
    // set the textarea's value to be the saved content, or a default if there is no saved content
        .val(button.data('textContent') || 'This is my comment field\'s text')
        // set up a keypress handler on the textarea
        .keypress(function(e) {
            if (e.which === 13) { // if it's the enter button
                e.preventDefault(); // ignore the line break
                button.data('textContent', this.value); // save the content to the button
                $(this).remove(); // remove the textarea
            }
        }).appendTo(parent_parent); // add the textarea to the document


});

1 个答案:

答案 0 :(得分:0)

由于绑定已经开启,所以#ed; -edit-comment&#39;这是&#39; .tweet-data&#39;的孩子。封装每个单独的条目(从你的逻辑的外观)我建议做

$(this).closest('.tweet-data').find('.entry-content').text();

您当前的逻辑是针对“父母”的最后一篇文章。元件。因此,如果您尝试单击除最后一个之外的推文数据上的编辑注释选项,则您的逻辑很可能无效。通过单击哪个元素进行上下文查找将解决这个潜在的问题。