提问者:小点点

jQuery显示嵌套在div内段落中的文本


我有超文本标记语言,看起来像这样:

<div class='textbox' data-title='Sometitle'>
<div class='textareaedit'>
     <p><strong>Test sample text</strong></p>
</div>
</div>

我有时会有一些不同的代码

我想遍历页面上的每个textbox,获取其标题以及嵌套在

$('.textbox').each(function() {
    $this = $(this);
    console.log($this.attr('data-title')+ ":\n");
    $this.children('textareadit').children('p').each(function(){
        console.log($(this).html()); // not giving any output, it's blank
    });
});

我也尝试了$(this). text(),但没有区别。您可能认为此示例中包含示例文本


共3个答案

匿名用户

传递给children()函数的类选择器缺少在类名之前。类名称中还有一个拼写错误,textareadit应该是textareaedit

$this.children('.textareaedit').children('p').each(function(){
    console.log($(this).html()); // not giving any output, it's blank
});

例子:http://jsfiddle.net/aWRaS/

匿名用户

您可以使用find()方法来定位它。试着这样做:

$this.find('.textareadit > p').each(function(){
    console.log($(this).html());
});

匿名用户

.children('textareadit')

应改为:

.children('.textareadit')

当你选择一个班级时