【问题标题】:Wrap is not working on dynamically loaded element. Why?Wrap 不适用于动态加载的元素。为什么?
【发布时间】:2022-02-04 18:43:31
【问题描述】:

wrap(); 本身运行良好,但在 load(); 之后调用它时就不行了。

可以做些什么来让它发挥作用?我试图将wrap(); 代码放在一个单独的 .js 文件中,但这不起作用。我也尝试将wrap();加载到里面:$( window ).on( "load", function() { ... }),然后放到html的页脚里面,也没有解决问题。

这是我的代码:

$('.page-id-202 #partner, .page-id-201 #partner').load('https://webhelytervezo.hu/ #gallery-1');
     $(".gallery-item").each(function() {
     $(this).wrap($("<a/>").attr("href", "http://" + $(".gallery-caption", this).text().trim()));
     $("#gallery-1 a").attr('target','_blank');
});
figcaption{
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="gallery-1" class="gallery galleryid-9 gallery-columns-3 gallery-size-full"><figure class="gallery-item">
            <div class="gallery-icon landscape">
                <img width="200" height="74" src="https://webhelytervezo.hu/wp-content/uploads/2022/01/cup_revolution_logo.jpg" class="attachment-full size-full" alt="" loading="lazy" aria-describedby="gallery-1-49">
            </div>
                <figcaption class="wp-caption-text gallery-caption" id="gallery-1-49">
                www.web.cuprevolution.eu
                </figcaption></figure><figure class="gallery-item">
            <div class="gallery-icon landscape">
                <img width="200" height="74" src="https://webhelytervezo.hu/wp-content/uploads/2022/01/areklam.jpg" class="attachment-full size-full" alt="" loading="lazy" aria-describedby="gallery-1-39">
            </div>
                <figcaption class="wp-caption-text gallery-caption" id="gallery-1-39">
                www.areklam.hu
                </figcaption></figure><figure class="gallery-item">
            <div class="gallery-icon landscape">
                <img width="200" height="74" src="https://webhelytervezo.hu/wp-content/uploads/2022/01/coca_cola.jpg" class="attachment-full size-full" alt="" loading="lazy" aria-describedby="gallery-1-48">
            </div>
                <figcaption class="wp-caption-text gallery-caption" id="gallery-1-48">
                www.coca-cola.hu
                </figcaption></figure><figure class="gallery-item">
            <div class="gallery-icon landscape">
                <img width="200" height="74" src="https://webhelytervezo.hu/wp-content/uploads/2022/01/arkad.jpg" class="attachment-full size-full" alt="" loading="lazy" aria-describedby="gallery-1-40">
            </div>
                <figcaption class="wp-caption-text gallery-caption" id="gallery-1-40">
                www.arkad.hu
                </figcaption></figure><figure class="gallery-item">
            <div class="gallery-icon landscape">
                <img width="200" height="74" src="https://webhelytervezo.hu/wp-content/uploads/2022/01/besttv.jpg" class="attachment-full size-full" alt="" loading="lazy" aria-describedby="gallery-1-41">
            </div>
                <figcaption class="wp-caption-text gallery-caption" id="gallery-1-41">
                www.besttv.hu
                </figcaption></figure> 
        </div>

编辑 1# 和 2#:

我需要将每个#gallery-1.gallery-items 从https://webhelytervezo.hu/ 加载到具有.page-id-202 类(https://webhelytervezo.hu/en/) 的页面的#partner div 和具有@ 类的页面987654336@ (https://webhelytervezo.hu/en/)。该代码仅适用于主页(内容未加载但无需加载)。

编辑 3#

现在代码是:

jQuery(document).ready(function( $ ){
$('.page-id-202 #partner, .page-id-201 #partner').load('https://webhelytervezo.hu/ #gallery-1', function(){
      $(".gallery-item").each(function() {
          $(this).wrap($("<a/>").attr("href", "http://" + $(".gallery-caption", this).text().trim()));
          $("#gallery-1 a").attr('target','_blank');
     })
});
  $(".gallery-item").each(function() {
          $(this).wrap($("<a/>").attr("href", "http://" + $(".gallery-caption", this).text().trim()));
          $("#gallery-1 a").attr('target','_blank');
     })
      });

如何简化?

【问题讨论】:

    标签: javascript html dom jquery-load jquery-wrap


    【解决方案1】:

    Load 是异步函数。将代码放入 load 方法的回调函数中。

    $('.page-id-202 #partner, .page-id-201 #partner').load('https://webhelytervezo.hu/ #gallery-1', function(){
          $(".gallery-item").each(function() {
              $(this).wrap($("<a/>").attr("href", "http://" + $(".gallery-caption", this).text().trim()));
              $("#gallery-1 a").attr('target','_blank');
         })   
    })
    

    【讨论】:

    • 嗯,是的,这应该是正确的答案,但它不起作用。请查看网站here
    • 如果你去那里看看源代码:view-source:webhelytervezo.hu/en 你可以看到没有 id="gallery-1" 元素所以你不能加载它.我猜他们是通过 js 加载的。
    • 确实是通过JS加载的。
    • 虽然webhelytervezo.hu 在这里。它应该工作......
    • @iorgu 我会给你最后的建议。使用 VScode 或其他一些好的代码编辑器/IDE。如果您使用它,您会立即看到缺少一个括号。我在所见即所得的编辑器中错过了它。
    【解决方案2】:

    您需要将其包裹在img 标签周围,而不是其前身:

    $('.page-id-202 #partner, .page-id-201 #partner').load('https://webhelytervezo.hu/ #gallery-1');
    $(".gallery-item .gallery-icon img").each(function() {
        $(this).wrap($("<a/>").attr("href", "http://" + $(".gallery-caption", this.parentNode.parentNode).text().trim()));
        $("#gallery-1 a").attr('target','_blank');
    });
    figcaption{
    display: none;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="gallery-1" class="gallery galleryid-9 gallery-columns-3 gallery-size-full"><figure class="gallery-item">
                <div class="gallery-icon landscape">
                    <img width="200" height="74" src="https://webhelytervezo.hu/wp-content/uploads/2022/01/cup_revolution_logo.jpg" class="attachment-full size-full" alt="" loading="lazy" aria-describedby="gallery-1-49">
                </div>
                    <figcaption class="wp-caption-text gallery-caption" id="gallery-1-49">
                    www.web.cuprevolution.eu
                    </figcaption></figure><figure class="gallery-item">
                <div class="gallery-icon landscape">
                    <img width="200" height="74" src="https://webhelytervezo.hu/wp-content/uploads/2022/01/areklam.jpg" class="attachment-full size-full" alt="" loading="lazy" aria-describedby="gallery-1-39">
                </div>
                    <figcaption class="wp-caption-text gallery-caption" id="gallery-1-39">
                    www.areklam.hu
                    </figcaption></figure><figure class="gallery-item">
                <div class="gallery-icon landscape">
                    <img width="200" height="74" src="https://webhelytervezo.hu/wp-content/uploads/2022/01/coca_cola.jpg" class="attachment-full size-full" alt="" loading="lazy" aria-describedby="gallery-1-48">
                </div>
                    <figcaption class="wp-caption-text gallery-caption" id="gallery-1-48">
                    www.coca-cola.hu
                    </figcaption></figure><figure class="gallery-item">
                <div class="gallery-icon landscape">
                    <img width="200" height="74" src="https://webhelytervezo.hu/wp-content/uploads/2022/01/arkad.jpg" class="attachment-full size-full" alt="" loading="lazy" aria-describedby="gallery-1-40">
                </div>
                    <figcaption class="wp-caption-text gallery-caption" id="gallery-1-40">
                    www.arkad.hu
                    </figcaption></figure><figure class="gallery-item">
                <div class="gallery-icon landscape">
                    <img width="200" height="74" src="https://webhelytervezo.hu/wp-content/uploads/2022/01/besttv.jpg" class="attachment-full size-full" alt="" loading="lazy" aria-describedby="gallery-1-41">
                </div>
                    <figcaption class="wp-caption-text gallery-caption" id="gallery-1-41">
                    www.besttv.hu
                    </figcaption></figure> 
            </div>

    【讨论】:

    • 不,事实并非如此。我已经编辑了这个问题,希望能更好地理解。
    • @iorgu,所以当加载 webhelytervező 时,可能会出现一些新项目并且没有正确包装?在我的 sn-p 中,一个锚点成功地包裹在 reklám 周围,如果我删除了 target 属性,它甚至可以从 sn-p 开始工作。
    • @Lajos_Arpad 不,是的。我想将#gallery-1 div 加载到寻址页面的#partner div 中(运行良好),然后然后将包装代码应用到 webhelytervezo.hu/en 上的这个加载部分和 /de。
    • 您的回答是相关的。在应用主要问题的解决方案后,我才意识到这一点。 img 标记上的包装效果更好,为此我还必须将它与 this.parentNode.parentNode 选择器一起使用 - 正如你所写的那样。
    • @iorgu 感谢您告诉我。重要的是它现在可以工作。这种包装不太容易发生结构变化,并且是一种更稳定的方法来应对这种需求,至少根据我的经验。与在前一个和下一个之间添加标签相比,您不太可能将其他项目包装在内标签周围,而是在包装标签内。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-13
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    相关资源
    最近更新 更多