[英]How to populate href value from src value of an image?
I know this is super simple, but I can't work it out (I'm a designer). 我知道这很简单,但我无法解决(我是一名设计师)。
I want to simply populate the href attribute value of the <a>
tag, with the value of the src attribute of the <img>
tag. 我想简单地使用
<img>
标记的src属性的值填充<a>
标记的href属性值。 So the image links to itself basically. 所以图像基本上链接到自己。
the image src will be updated through a super simple CMS and I want the link to dynamically update, instead of the user having to update the URL also. 图像src将通过一个超级简单的CMS进行更新,我希望链接能够动态更新,而不是用户也必须更新URL。
I tried something like this, but it didn't work. 我尝试过这样的东西,但它没有用。
<a rel="lightbox[job1]" href="()document.getElementById("gal1_img1").src">
<img id="gal1_img1" src="images/gallery/job1/image-1.jpg">`enter code here`
</a>
You cannot inline JavaScript inside attributes the way you have currently. 您不能以当前的方式内联JavaScript内部属性。 You can add a script to the page to do this:
您可以向页面添加脚本来执行此操作:
<a id="link" rel="lightbox[job1]" href="#">
<img id="gal1_img1" src="images/gallery/job1/image-1.jpg">`enter code here`
</a>
<script>
document.getElementById("link").href = document.getElementById("gal1_img1").src;
</script>
try this as your <a>
's href: 试试这个你的
<a>
的href:
href="javascript:window.location=this.getElementsByTagName('img')[0].src;"
Assuming there's only ever one image inside these <a>
tags, this should work for anyone who's NOT running with javascript disabled. 假设这些
<a>
标签中只有一个图像,这适用于没有禁用javascript运行的任何人。
try this. 尝试这个。
var att = $('#gal1_img1').attr('src');
$("a[rel*='lightbox[job1]']").attr('href',att);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.