如何针对正确的孩子?

时间:2017-01-03 15:56:56

标签: javascript jquery html jquery-selectors

点击各个 添加btn

后,我试图将整个iframe添加到数组中

HTML

<div class="col-sm-4">
   <div class="thumbnail">
      <div class="embed-responsive embed-responsive-16by9">
        <iframe class="embed-responsive-item" src="https://www.youtube.com/embed/2tM1LFFxeKg" frameborder="0" allowfullscreen=""></iframe>
      </div>
    <div class="caption">
       <p>Duration: 
         <span class="video-time">4:20</span>
       </p>
       <button type="button" class="btn btn-danger btn-block btn_video">
         <strong>ADD</strong>
       </button>
    </div>
   </div>
</div>

<div class="col-sm-4">
   <div class="thumbnail">
      <div class="embed-responsive embed-responsive-16by9">
        <iframe class="embed-responsive-item" src="https://www.youtube.com/embed/2tM1LFFxeKg" frameborder="0" allowfullscreen=""></iframe>
      </div>
    <div class="caption">
       <p>Duration: 
         <span class="video-time">4:20</span>
       </p>
       <button type="button" class="btn btn-danger btn-block btn_video">
          <strong>ADD</strong>
       </button>
    </div>
   </div>
</div>

JS

$('body').on('click', '.btn_video', function () {
  var urls = [];
  $('.btn_video').each(function () {
     urls.push($(this).parent().parent().parent().find(".thumbnail").closest(".embed-responsive").html());
  });
  var str = '';
  urls.forEach(function (url) {
    str += url;
  });
  $('#usp-custom-5').val(str);
});

我打印出来的是:

  

undefinedundefinedundefinedundefinedundefinedundefined

2 个答案:

答案 0 :(得分:2)

点击监听器中,您不需要each()功能,您可以使用以下选项 iframe html

$(this).closest(".thumbnail").find(".embed-responsive")

见下面的演示:

var urls = [];
$('body').on('click', '.btn_video', function() {
  urls.push($(this).closest(".thumbnail").find(".embed-responsive").html());
  $('#usp-custom-5').text(urls.join(' '));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-sm-4">
  <div class="thumbnail">
    <div class="embed-responsive embed-responsive-16by9">
      <iframe class="embed-responsive-item" src="https://www.youtube.com/embed/2tM1LFFxeKg" frameborder="0" allowfullscreen=""></iframe>
    </div>
    <div class="caption">
      <p>Duration:
        <span class="video-time">4:20</span>
      </p>
      <button type="button" class="btn btn-danger btn-block btn_video">
        <strong>ADD</strong>
      </button>
    </div>
  </div>
</div>

<div class="col-sm-4">
  <div class="thumbnail">
    <div class="embed-responsive embed-responsive-16by9">
      <iframe class="embed-responsive-item" src="https://www.youtube.com/embed/2tM1LFFxeKg" frameborder="0" allowfullscreen=""></iframe>
    </div>
    <div class="caption">
      <p>Duration:
        <span class="video-time">4:20</span>
      </p>
      <button type="button" class="btn btn-danger btn-block btn_video">
        <strong>ADD</strong>
      </button>
    </div>
  </div>
</div>

<div id="usp-custom-5"></div>

答案 1 :(得分:0)

这个怎么样?

$('body').on('click', '.btn_video', function () {
  var urls = [];
  $('.btn_video').each(function () {
      urls.push($(this).closest(".thumbnail").find(".embed-responsive").html());
  });

  var str = '';
  urls.forEach(function (url) {
         str += url;
  });
  $('#usp-custom-5').val(str);
});