选择“p文本jquery”

select p text jquery

本文关键字:jquery 文本 选择      更新时间:2023-09-26

我正在从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>";

我可以用这个表达式=>得到表CCD_ 1

$(".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>

等等。。。

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

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

或:

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