使用 jQuery 获取 <p> 标签内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24841893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get <p> tag content using jQuery
提问by Alex Pereira
I want to get the content of the `P' tag from the following html code using jQuery.
我想使用 jQuery 从以下 html 代码中获取“P”标签的内容。
<div id="div_id_disciplina" class="form-group ">
<label for="id_disciplina" class="control-label ">
Disciplina
</label>
<div class="">
<ul class="token-input-list-facebook">
<li class="token-input-token-facebook">
<p>áudio e Vídeo</p>
<span class="token-input-delete-token-facebook">×</span>
</li>
<li class="token-input-input-token-facebook">
<input type="text" autocomplete="off" id="token-input-id_disciplina">
</li>
</ul>
<textarea class="form-control" id="id_disciplina" name="disciplina"></textarea>
</div>
How to achieve it? Thanks in advance.
如何实现?提前致谢。
回答by Wio
Query it with $('p')
and use the .text()
function to get the innerHTML as text.
查询它$('p')
并使用该.text()
函数将innerHTML 作为文本获取。
$('p').text()
回答by Josh
$('p').text()
would get the text inside the paragraph tag.
$('p').text()
将获得段落标签内的文本。
回答by Kyle Emmanuel
You can do it like this:
你可以这样做:
// If you want to get the html content
$(".token-input-token-facebook p").html();
// Or if you want to get the text
$(".token-input-token-facebook p").text();
回答by viniciuswebdev
//when document is load
$(function(){
//get the children of element
var p = $(".token-input-token-facebook").find('p');
//and then get the text
alert(p.text());
})
Live example: http://jsfiddle.net/r39qu/
现场示例:http: //jsfiddle.net/r39qu/
回答by David Barker
Just find it and get it's text.
只需找到它并获取它的文本。
var value = $("ul li.token-input-token-facebook > p").text();