使用 jquery 获取 div 内容的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19581683/
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 value of div content using jquery
提问by redcoder
I have the following html and I want to get the value of the div which is "Other" How can I do this with jQuery?
我有以下 html,我想获取“其他”的 div 值,我该如何使用 jQuery 执行此操作?
<div class="readonly_label" id="field-function_purpose">
Other
</div>
回答by Arun P Johny
回答by Roopendra
回答by Barun
$('#field-function_purpose')
will get you the element with ID field-function_purpose
, which is your div element. text()
returns you the content of the div.
$('#field-function_purpose')
会给你带 ID 的元素field-function_purpose
,这是你的 div 元素。text()
返回 div 的内容。
var x = $('#field-function_purpose').text();
回答by Ishan Jain
your div looks like this:
你的 div 看起来像这样:
<div class="readonly_label" id="field-function_purpose">Other</div>
With jquery you can easily get inner content:
使用 jquery,您可以轻松获取内部内容:
Use .html()
: HTML contents of the first element in the set of matched elements or set the HTML contents of every matched element.
使用.html()
: 匹配元素集中第一个元素的 HTML 内容或设置每个匹配元素的 HTML 内容。
var text = $('#field-function_purpose').html();
Read more about jquery .html()
or
或者
Use .text()
: Get the combined text contents of each element in the set of matched elements, including their descendants, or set the text contents of the matched elements.
用途.text()
:获取匹配元素集合中每个元素的组合文本内容,包括其后代,或者设置匹配元素的文本内容。
var text = $('#field-function_purpose').text();
回答by Ketan Savaliya
Try this to get value of div content using jquery.
尝试使用 jquery 获取 div 内容的值。
$(".showplaintext").click(function(){
alert($(".plain").text());
});
// Show text content of formatted paragraph
$(".showformattedtext").click(function(){
alert($(".formatted").text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="plain">Exploring the zoo, we saw every kangaroo jump and quite a few carried babies. </p>
<p class="formatted">Exploring the zoo<strong>, we saw every kangaroo</strong> jump <em><sup> and quite a </sup></em>few carried <a href="#"> babies</a>.</p>
<button type="button" class="showplaintext">Get Plain Text</button>
<button type="button" class="showformattedtext">Get Formatted Text</button>
Taken from @ Get the text inside an element using jQuery