选择p text jquery

时间:2012-08-24 09:09:59

标签: javascript jquery select text

我正在从mysql中检索数据,例如this structure =>

echo "<table id='postI" . $chat_row['id'] . "'>";
echo "<tr>";
      echo "<td><p class='post_author'>" . $chat_row['user_name'] . "</p><a href='javascript: void(0)' class='edit_post_a'><div class='edit_post'></div></a></td>";
echo "</tr>";
echo "<tr>";
      echo "<td><p class='posted_on'>" . $chat_row['posted_on'] . "</p></td>";
echo "</tr>";
echo "<tr>";
      echo "<td><br></td>";
echo "</tr>";
      echo "<tr>";
echo "<td><p class='posted_msg'>" . $chat_row['message'] . "</p></td>";
      echo "</tr>";
echo "</table>";

我可以使用此表达式=&gt;

获取表格id
$(".edit_post_a").bind("click",function(){
    var tb_id = $(this).closest("table").attr("id"); //  works , gets the id of the table
    alert($(tb_id + ".posted_msg").text()); // don't work, gets nothing , just alert box 
    });

我的问题是想知道第三个p的文字,我尝试了什么=&gt;

alert($("#" +tb_id + ".posted_msg").text());
alert($("#" +tb_id).find("p").last().text());
alert($("#" +tb_id + " p:nth-child(3)").text());

什么都行不通,得到空警报框,我不知道什么是错的?我该怎么解决?谢谢

HTML code =&gt;

<table id='postI245'>
<tr>
    <td><p class='post_author'>USER</p><a href='javascript: void(0)' class='edit_post_a'><div class='edit_post'></div></a>
    </td>
</tr>
<tr>
    <td><p class='posted_on'>DATE</p></td>
</tr>
<tr>
    <td><br></td>
</tr>
<tr>
    <td><p class='posted_msg'>MSG</p></td>
</tr>
</table>

依旧......

1 个答案:

答案 0 :(得分:2)

您的选择器不会选择任何内容,您应添加#以按ID选择元素。

var txt = $('#'+tb_id + " .posted_msg").text();

或:

var txt = $(this).closest("table").find("p:eq(2)").text()