使用jquery获取div内容的值
我有以下的HTML,我想得到div的值是“其他”我怎么能用jQuery做到这一点?
Other
使用.text()来提取div的内容
var text = $('#field-function_purpose').text()
您可以在jquery中使用.text()
获取div内容
var divContent = $('#field-function_purpose').text(); console.log(divContent);
小提琴
$('#field-function_purpose')
将为您提供ID field-function_purpose
元素,这是您的div元素。 text()
返回div的内容。
var x = $('#field-function_purpose').text();
你的div看起来像这样:
Other
使用jquery,您可以轻松获得内部内容:
使用.html()
:匹配元素集中第一个元素的HTML内容,或者设置每个匹配元素的HTML内容。
var text = $('#field-function_purpose').html();
阅读更多关于jquery .html()的信息
要么
使用.text()
:获取匹配元素集合中每个元素的组合文本内容,包括它们的后代,或者设置匹配元素的文本内容。
var text = $('#field-function_purpose').text();
阅读更多关于jquery .text()的信息
尝试使用jquery获取div内容的值。
$(".showplaintext").click(function(){ alert($(".plain").text()); }); // Show text content of formatted paragraph $(".showformattedtext").click(function(){ alert($(".formatted").text()); });
Exploring the zoo, we saw every kangaroo jump and quite a few carried babies.
Exploring the zoo, we saw every kangaroo jump and quite a few carried babies.