在除第一个子节点之外的所有子节点的文本之间添加space

Add space in between the texts of all the child nodes except the 1st one

我需要在单击时抓取所有子节点的文本,第一个子节点除外。 但是,当我尝试这样做时,我无法在 2 个子节点的文本之间添加 space。

例如,child1 的文本:文本 1,child2 的文本:文本 2,child3 的文本:文本 3。

我想要的:文字2文字3

我得到的是:文本 2 文本 3

这是我的代码:

$(document).ready(function(){
  $('.container').click(function(){
  var myText = $(this).children(":not(.nograb)").text();
  console.log(myText);
  });   
});
.container{
  border: 1px solid black;
  padding: 10px;
  margin-top: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="container">
  <p class="nograb">Don't grab this!</p>
  <p>My text1</p>
  <span>My text2</span>
</div>
<div class="container">
  <p class="nograb">Don't grab this!</p>
  <p>My text3</p>
  <span>My text4</span>
</div>

您可以填充数组 ["My text1", "My text2"],然后执行 .join(" ") 操作,这将仅在 关节之间插入 " " :)导致这个字符串:

"My text1 My text2"

$('.container').click(function() {

  var myText = $(this).children(":not(.nograb)").map(function() {
    return $(this).text();
  }).get().join(" ");
  
  console.log(myText);

});
.container {
  border: 1px solid black;
  padding: 10px;
  margin-top: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="container">
  <p class="nograb">Don't grab this!</p>
  <p>My text1</p>
  <span>My text2</span>
</div>
<div class="container">
  <p class="nograb">Don't grab this!</p>
  <p>My text3</p>
  <span>My text4</span>
</div>