微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

添加特定的锚链接一个类

如何解决添加特定的锚链接一个类

如何向我的锚链接添加课程?

我的 html 代码如下所示:

<div class="et_pb_blurb_content">
  <div class="et_pb_main_blurb_image">
    <a href="video.mp4">
      <span class="et_pb_image_wrap">
        <img loading="lazy" width="128" height="128" src="2021/02/cover.png" alt="" class="et-waypoint et_pb_animation_off wp-image-17" />
      </span>
    </a>
  </div>
  <div class="et_pb_blurb_container">
    <h4 class="et_pb_module_header">
      <a href="video.mp4">Watch</a>
    </h4>
  </div>
</div>

使用我想要实现的 javascript 代码,它添加了锚链接类:

<div class="et_pb_blurb_content">
  <div class="et_pb_main_blurb_image">
    <a href="video.mp4" class="vp-a">
      <span class="et_pb_image_wrap">
        <img loading="lazy" width="128" height="128" src="2021/02/cover.png" alt="" class="et-waypoint et_pb_animation_off wp-image-17" />
      </span>
    </a>
  </div>
  <div class="et_pb_blurb_container">
    <h4 class="et_pb_module_header">
      <a href="video.mp4" class="vp-a">Watch</a>
    </h4>
  </div>
</div>

我使用 wordpress divi 主题并且无法添加类来锚定视频链接

解决方法

可以使用以下内容:

// grab all the parent items by classname
const linkParents = document.querySelectorAll('.et_pb_module_header');

// iterate over parents
[...linkParents].forEach((parent) => {
  // grab the anchor tag from the parent
  const link = parent.querySelector('a');

  // add your classname to the anchor tag
  link.classList.add('vp-a');
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。