1

我正在从 mysql 中检索数据,就像这种结构 =>

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

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,我试过了=>

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

没有任何效果,得到空的警报框,我不知道出了什么问题?我该如何解决?谢谢

HTML 代码 =>

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

等等 ...

4

1 回答 1

2

您的选择器没有选择任何内容,您应该添加#以按 id 选择元素。

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

或者:

var txt = $(this).closest("table").find("p:eq(2)").text()
于 2012-08-24T09:13:04.337 回答