[英]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.