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>
5 个解决方案
#1
29
Use .text() to extract the content of the div
使用.text()来提取div的内容
var text = $('#field-function_purpose').text()
#2
#3
0
$('#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();
#4
0
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()
阅读更多关于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();
Read more about jquery .text()
阅读更多关于jquery .text()的信息
#5
0
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
取自@使用jQuery获取元素内的文本
#1
29
Use .text() to extract the content of the div
使用.text()来提取div的内容
var text = $('#field-function_purpose').text()
#2
3
You can get div content using .text()
in jquery
您可以在jquery中使用.text()获取div内容
var divContent = $('#field-function_purpose').text();
console.log(divContent);
#3
0
$('#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();
#4
0
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()
阅读更多关于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();
Read more about jquery .text()
阅读更多关于jquery .text()的信息
#5
0
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
取自@使用jQuery获取元素内的文本