在jQuery中从DIV获取文本

时间:2014-11-30 15:21:59

标签: javascript jquery css

我有以下类型的DIV:

 <div class='entry'>
                <p class='entry-text'>собаки играют вместе <a title="Search private" class="app-context-link" href="/contexts/private">@private</a></p>
                <p class='entry-date'>14172869383540000</p>
                <p class='entry-menu'>&nbsp;&nbsp;&nbsp;<a class="app-link-menu" href="#">show filtered</a></p>
                <div class='separator'>&nbsp;</div>
</div>

我有一个代码可以取出DIV的内容,如下所示,但是如何更改它以便它取出内部文本&#39; entry-text&#39;只上课?

  $(".entry").on('dblclick', function(e) {

        e.preventDefault();

        // Get the statement into the edit box at the top
        $("#statement").val(e.currentTarget.innerText);
} 

谢谢!

3 个答案:

答案 0 :(得分:2)

这应该这样做:

$(".entry").on('dblclick', function(e) {
    e.preventDefault();

    // Get the statement into the edit box at the top
    $("#statement").val($('.entry-text', e.currentTarget).text());
} 

$('.entry-text', e.currentTarget)选择entry-text元素中e.currentTarget类的所有元素,然后在其上调用.text()

来源:

答案 1 :(得分:1)

$(".entry").on('dblclick', function(e) {
    e.preventDefault();

    // Get the statement into the edit box at the top
    $("#statement").html($('.entry-text', e.currentTarget).text());
} 

.val()用于输入框和表单 .html()用于选择文本

答案 2 :(得分:0)

e.currentTarget 通常等于。所以这也有效。

$(".entry").on('dblclick', function(e) {
    e.preventDefault();

    // Get the statement into the edit box at the top
    $("#statement").val($('.entry-text', this).text());
}