【发布时间】:2014-08-09 05:51:16
【问题描述】:
使用 Jquery,我想将具有特定类的所有元素的内容放入一个变量中。这是一个例子:
<div class="foo">
<div class="bar"><p>First set of text</p></div>
<div class="bar"><p>Second set of text</p></div>
<div class="bar"><p>Third set of text</p></div>
<div class="bar"><p>N set of text</p></div>
</div>
我想将所有具有“bar”类的 div 元素的内容放入一个名为“full”的变量中。我已经尝试了以下代码,但显然只抓取了第一个 div。
var full = $( ".bar" ).html();
我希望我的字符串变量的内容是
"<p>First set of text</p><p>Second set of text</p><p>Third set of text</p><p>N set of text</p>"
如果尝试在互联网上查找但找不到答案。我正在考虑使用 .each 方法,但我的几次尝试效果不佳。有什么想法吗?
谢谢!
【问题讨论】:
标签: jquery