在从src传递的Href中插入属性

时间:2019-05-24 12:11:15

标签: javascript jquery

大家好,我有这个由Tapatalk插件生成的HTML

<a href="https://uploads.tapatalk-cdn.com/20190521/77b84bf29111fc4dffc72261951.jpg" target="_blank" rel="noopener">
    <img alt="77b84bf29111fc4dffc72261951.jpg" data-imageproxy-source="https://uploads.tapatalk-cdn.com/20190521/77b84bf29111fc4dffc72261951.jpg" src="/applications/core/interface/imageproxy/imageproxy.php?img=https://uploads.tapatalk-cdn.com/20190521/77b84bf29111fc4dffc72261951.jpg">
    </a>

我需要从此img标签返回href才能添加:

<a class="ipsAttachLink ipsAttachLink_image" data-fileext="jpg" data-fileid="" href="" rel="" data-ipslightbox="" data-ipslightbox-group="g58000">***</a>

这些标签可以让我在灯箱中打开图片。有没有办法将这些标签插入href中的src中的tapatalk这个词?

1 个答案:

答案 0 :(得分:1)

您可以获取所有包含a的{​​{1}}元素和包含'tapatalk'的img。遍历所有内容并根据需要添加属性。

img.src
//REM: This finds all images contained in an anchor (<a/>) with a [src] containing "tapatalk"
for(var tListOfElements=document.querySelectorAll('a > img[src*="tapatalk"]'), i=0, j=tListOfElements.length; i<j; i++){
  var tAnchor = tListOfElements[i].parentNode; //REM: The anchor element
  tAnchor.setAttribute('whatever', 'avalue') //REM: Setting attributes
}