简体   繁体   English

JS 需要帮助在 img 上添加 href

[英]JS Need help to add href on img

i'm using Innova Content Builder to record my webpage.我正在使用 Innova Content Builder 来记录我的网页。 It's contains a module called saveimage.php which transforms binary images to jpg files.它包含一个名为 saveimage.php 的模块,它将二进制图像转换为 jpg 文件。 This works perfectly.这完美地工作。

Now, i would like to encapsulate these img whith href links, but it doesn't works.现在,我想用href链接封装这些img,但它不起作用。

Here is my code:这是我的代码:

parent.document.getElementById('img-" . $count . "').setAttribute('src','" . $urlpath . $image . "');
parent.document.getElementById('img-" . $count . "').setAttribute('alt','".$image."');
var myLink = document.createElement('a');
myLink.setAttribute('href','http://www.google.fr');
parent.document.getElementById('img-" . $count . "').appendChild(myLink);

This code is placed into a body onload function.这段代码放在一个body onload function中。 I think it's a parentality problem.我觉得是亲子关系的问题。

Can you help me?你能帮助我吗? Thanks !谢谢 !

Use this:用这个:

<script>
    parent.document.getElementById('img-" . $count . "').setAttribute('src','" . $urlpath . $image . "');
    parent.document.getElementById('img-" . $count . "').setAttribute('alt','".$image."');
var myLink = document.createElement('a');
myLink.setAttribute('href','http://www.google.fr');
    parent.document.getElementById('img-" . $count . "').parentNode.insertBefore(myLink, parent.document.getElementById('img-" . $count . "'));
    myLink.appendChild(parent.document.getElementById('img-" . $count . "'));
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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