简体   繁体   中英

Insert attribute in Href passing from src

Hi guys I have this HTML generated by the Tapatalk plugin

<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>

I need to go back to the href from this img tag to be able to add:

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

those tags would allow me to open the image in a lightbox. is there a way to insert these tags to href looking in the src the word tapatalk?

You can fetch all the a elements containing an img and the img.src containing 'tapatalk'. Loop through all and add attributes according to your needs.

 //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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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