[英]Insert attribute in Href passing from src
嗨,大家好,我有这个由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
元素和包含“ img.src
”的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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.