首页 > 解决方案 > 如何使用 jquery 在 .each() 中设置特定 id 的特定元素

问题描述

我有这么多DIV<p>我想设置 .each() 尤其DIV's <p>是我在下面的代码中完成的。

var Selected_type = 'business'; //which comes dynamically
var para_id = Selected_type + '_ltr';

var text = "";
$(para_id + "p").each(function(index) {
  if (index < count) {
    text += '<p>' + ($(this).text()).trim() + "</p><br>";
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="col-xs-12" id="business_ltr" style="max-height: none; display: none;">
  <p>Learn how to transform your life for the better with 16 of the Web’s favorite entrepreneurship and leadership articles from this year.</p>

  <p>Mental fortitude is essential for entrepreneurship, and in this wildly popular Forbes article, </p>
</div>

<div class="col-xs-12" id="study_ltr" style="max-height: none; display: none;">
  <p>With all the emphasis placed on education in our society it is remarkable to realize that there has never been an actual technology of study or a technology of education. </p>

  <p>The first little gate that has to be opened to embark upon study is the willingness to know. .</p>
</div>

但这显示我未定义并且没有显示任何结果。

但如果我设置$("p" ).each()它正在工作,但它得到了一切<p>,但我想要特别DIV's <p>

谁能帮我解决这个问题

标签: javascriptjqueryhtml

解决方案


推荐阅读