我试图读取段落标签内的内容,该标签是div的子节点,并将这些值存储到数组中并传递给控制器
这是代码:

 <div class = "proposal">
    <p> one </p>
    <p> two </p>
    <p> three </p>
    <p> three </p>
    <p> three </p>
    <p> three </p>
    </div>
    Please help me to get the content of paragraph and store into an array using jquery.
I am trying to get a solution in this way:

var proposal = $("#Proposal").contents().filter(function() {
                                return this.nodeType === 3;
                            }) .wrap( "<p></p>" ).end();


我得到了物体,但是,

for(i=0;i<proposal.length;i++){
    proposal = proposal[i].innerHTML
}


给我的错误是
 无法读取未定义的属性“长度”

最佳答案

var arr = $(".proposal p").map(function() {

  return $(this).text()

}).get();


console.log(arr)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="proposal">
  <p> one </p>
  <p> two </p>
  <p> three </p>
  <p> three </p>
  <p> three </p>
  <p> three </p>
</div>






使用.map()添加到数组中。
.text()与选择器$(".proposal p")->一起使用,表示元素元素的所有子级p与类提案

关于javascript - 将文本放入每个<p>内,该<p>是div的子节点,并存储在数组中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44542904/

10-12 15:58