简体   繁体   English

用jquery获取p标签的值

[英]Getting value of p tag with jquery

Im using jquery to try to take the value text in ap tag. 我使用jquery尝试在ap标签中获取值文本。

I set up an alert for testing to give me the value and it says "current stack is [object Object]" 我设置了一个测试警报给我值,它说“当前堆栈是[对象对象]”

My html for this is 我的HTML是这样的

<div id="player9">
    <div class="player_stats">
      <p class="player_name"></p>
      <p class="player_action"></p>
      <p class="player_money"></p>
   </div>
</div>

My alert code is: 我的提醒代码是:

alert("current stack is " + $("#player9").children(".player_stats").children("p.player_money"));

if I add .val() it comes back as undefined. 如果我添加.val()它会返回undefined。 These values were set upon page load by: 这些值是在页面加载时设置的:

$(whichseat[seat]).children(".hand").children("p.bet").text(bet);

"whichseat[seat] and bet are based into the function. They are setting correctly. “whereseat [座位]和下注基于该功能。他们正确设置。

If I use firebug and checkout that specific p tag it says: 如果我使用firebug并签出特定的p标签,它会说:

<p class="player_money">60000</p>

How do I correctly grab that value? 我如何正确地获取该值?

Any help would be greatly appreciated! 任何帮助将不胜感激!

You could use .html() or .text() 你可以使用.html().text()

alert("current stack is " + $("#player9").children(".player_stats").children("p.player_money").html());

Or: 要么:

alert("current stack is " + $("#player9").children(".player_stats").children("p.player_money").text());

Obviously the text method will grab just the text, where as the html will get the markup as well. 显然,文本方法只会抓取文本,因为html也会获得标记。 Demo Here 在这里演示

You can use the text() method. 您可以使用text()方法。 The val() method is used for getting input field's values. val()方法用于获取输入字段的值。 So, you will have: 所以,你将拥有:

alert("current stack is " + $("#player9").children(".player_stats").children("p.player_money").text());

Not sure what else I'm missing, but since you're setting the inner text with .text() , you get it using the same method. 不确定我还缺少什么,但由于你使用.text()设置内部文本,你可以使用相同的方法获得它。

If all else fails you may have to specify to get the inner text of exactly one element: 如果所有其他方法都失败了,您可能必须指定获取正好一个元素的内部文本:

$("#player9 > .player_stats > p.player_money:first").text()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM