我在页面上有一堆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`);
});
好像您只是反转了函数参数。上下文位于选择器之后,而不是之前:
$('iframe', this);