JQuery - 图库标题文本为 link 以包装图库项目

JQuery - Gallery caption text as link to wrap gallery items

我正在尝试 wrap() .gallery-itemstext(),即来自 .gallery-caption 的 url。我的问题是所有 url 都被提取并插入到 a href"=https:// 中。

使其正常工作的正确方法是什么?提前谢谢你。

以下是我的尝试:

$(".gallery-item").wrap($("<a/>").attr("href", "http://"+$(".gallery-caption").text().trim()));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<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/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.arkadbudapest.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><figure class="gallery-item">
            <div class="gallery-icon landscape">
                <img width="200" height="74" src="https://webhelytervezo.hu/wp-content/uploads/2022/01/bosch.jpg" class="attachment-full size-full" alt="" loading="lazy" aria-describedby="gallery-1-42">
            </div>          
      

我也尝试实现 $(this)... 但我可能用错了它。

您可以简单地使用每个循环,然后在该循​​环内使用 this,它将引用迭代的当前元素。

演示代码 :

$(".gallery-item").each(function() {
  $(this).wrap($("<a/>").attr("href", "http://" + $(".gallery-caption", this).text().trim()));
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<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/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.arkadbudapest.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>