简体   繁体   English

如何使用 jquery 中的特定 div 用最近的锚标签 url 更改当前锚标签 href

[英]how to change the current anchor tag href with the nearest anchor tag url with in specific div in jquery

Hii i am trying to change the anchor tag href with class media-url with the nearest anchor tage url which is inside field__item div, but i am getting undefined, please let me know where i am doing wrong.嗨,我正在尝试使用 class 媒体 URL 更改锚标签href,其中最近的锚标签 url 在 field__item div 内,但我越来越不确定,请让我知道我在哪里做错了。

  <div class="media media--blazy media--bundle--image media--image media--responsive is-b-loaded">        <a class="media-url" href="https://examplelist/item.com" target="_blank" rel="noopener nofollow noreferrer">
</a>
</div>

<div class="component-spotlight__inner">         
<header class="component-spotlight__title">
            <h2 class="component-spotlight__title-inner">Group Exercise Programs</h2>
            <h4 class="component-spotlight__eyebrow"></h4>
          </header>
 <div class="component-spotlight__content">
  <div class="field field--name-field-media field--type-link field--label-visually_hidden">
    <div class="image-url field__label visually-hidden">media image url</div>
              <div class="field__item"><a href="https://www.example.com/" target="_blank" rel="noopener nofollow noreferrer">https://www.example.com/</a></div>
     </div>
     </div>
     </div>

here is my jquery:这是我的 jquery:

  let mediaimageurl = $('.media-url').find('.field__item a').attr('href');
  $('.media-url').attr('href' , mediaimageurl);
    

You need to use closest() to get the parent container and then next() to get .component-spotlight__inner and finally use find() to get the container of your link.您需要使用最接近()来获取父容器,然后使用next()来获取.component-spotlight__inner ,最后使用find()来获取链接的容器。

 let mediaimageurl = $('.media-url').closest('.media--bundle--image').next('.component-spotlight__inner').find('.field__item a').attr('href'); $('.media-url').attr('href', mediaimageurl); console.log(mediaimageurl);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="media media--blazy media--bundle--image media--image media--responsive is-b-loaded"> <a class="media-url" href="https://examplelist/item.com" target="_blank" rel="noopener nofollow noreferrer"> </a> </div> <div class="component-spotlight__inner"> <header class="component-spotlight__title"> <h2 class="component-spotlight__title-inner">Group Exercise Programs</h2> <h4 class="component-spotlight__eyebrow"></h4> </header> <div class="component-spotlight__content"> <div class="field field--name-field-media field--type-link field--label-visually_hidden"> <div class="image-url field__label visually-hidden">media image url</div> <div class="field__item"><a href="https://www.example.com/" target="_blank" rel="noopener nofollow noreferrer">https://www.example.com/</a></div> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM