[使用“ this”在div中获取子元素-jQuery

问题描述 投票:0回答:1

我在页面上有一堆HTML容器,当我遍历以获取每个“ iframeHolder”时,我无法在持有人中获得iframe来为其传递属性。

我正在使用的脚本在HTML下方,关于如何正确获取子元素并更新属性而没有错误的任何建议?

  <div class="row iframeHolder>
    <div class="col-large col-centered position-relative">
      <div class="card mb-4 box-shadow">

          <iframe class="iframes">
            <p>Your browser does not support iframes.</p>
          </iframe>
      </div>
    </div>
  </div>



$('.iframeHolder').each(function() {
      var iframe = $(this, 'iframe');
      console.log(iframe);

      iframe.attr('src', `google.com`);  
});
jquery iframe attributes children
1个回答
1
投票

好像您只是反转了函数参数。上下文位于选择器之后,而不是之前:

$('iframe', this);
© www.soinside.com 2019 - 2024. All rights reserved.