【问题标题】:select p text jquery选择 p 文本 jquery
【发布时间】:2012-08-19 20:05:52
【问题描述】:

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

等等……

【问题讨论】:

  • 请发布您的渲染标记,而不是服务器端代码。

标签: javascript jquery select text


【解决方案1】:

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

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

或:

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

【讨论】:

  • 您是否也尝试过` alert($("#" + tb_id + ".posted_msg").html());` ?注意“#”的出现和函数text()已被html()替换
  • @Tornike 不客气,第一个不起作用,因为您只有一个类为 .posted_msg jsfiddle.net/SPmb6/1 的 p 标签
猜你喜欢
  • 1970-01-01
  • 2013-04-07
  • 2017-01-07
  • 1970-01-01
  • 2013-04-05
  • 2011-07-11
  • 1970-01-01
  • 2010-10-15
  • 2011-06-27
相关资源
最近更新 更多