gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:36:02 30 4
gpt4 key购买 nike

大家好,我有 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?

最佳答案

您可以获取包含 img 的所有 a 元素和包含“tapatalk”的 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
}
<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>

关于javascript - 在从 src 传递的 Href 中插入属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56292462/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com