简体   繁体   English

自动<a>从的src</a>获取href in <a>标记</a> <a><img></a> <a>标签</a>

[英]automatically get the href in <a> tag from the src of an <img> tag

This probably seems like a strange question at first, but I haven't managed to find another way to solve the problem. 起初这似乎是一个奇怪的问题,但我还没有找到其他解决问题的方法。 Apologies if this is long winded, but I wanted to try and explain exactly what I'm trying to do! 抱歉,这很长一段时间,但是我想尝试解释一下我要做什么!

Basically, I am using lightbox to display an image which when clicked, enlarges the image (this being what lightbox does..!) This is working fine, however: What lightbox actually does is load another image - useful so you can have two files, one small (thumbnail) and one large. 基本上,我正在使用灯箱显示图像,当单击该图像时,会放大图像(这就是灯箱的功能。。!)这很好,但是:灯箱的实际作用是加载另一个图像-有用,因此您可以拥有两个文件,一小(缩略图)和一大。 I could even put the same image in both locations and scale the small one using CSS/html - as I have here - the relevant piece of lightbox code is below: 我什至可以在两个位置都放置相同的图像,并使用CSS / html缩放较小的图像-正如我在这里-有关的灯箱代码如下:

<a href="image1.gif" rel="lightbox" title="Project one image">
<img  src="image1.gif" width="200px" alt="small version of image" /></a>

(I understand that it is preferable to have two seperate files for loading time etc, but in this case this will not work) (我知道最好有两个单独的文件来加载时间,等等,但是在这种情况下将不起作用)

But the problem I am having is that the image in the image tag needs to be able to be changed (to a completely different image), without anything else been changed - so without updating the href for the lightbox link. 但是我遇到的问题是,图像标记中的图像需要能够更改(更改为完全不同的图像), 而无需进行其他任何更改-因此,无需更新灯箱链接的href。 But the lightbox needs to show the same image as is brought up by the image tag- as it stands, both the img tag and a tag need to be changed to do this. 但是灯箱需要显示与image标签所显示的图像相同的图像-就其本身而言,img标签和标签都需要进行更改以实现此目的。 In this case, it will not be possible to edit the a tag - I am setting this up to allow another (fairly technophobic) person to replace the image using CushyCMS (a content editor) - this realistically only gives the ability to change the image itself, as I cannot rely on this person to also update the a tag (this is definitely not an option!) 在这种情况下,将无法编辑标签-我将其设置为允许其他(相当技术恐惧)的人使用CushyCMS(内容编辑器)替换图片-实际上,这仅提供了更改图片的功能本身,因为我不能依靠这个人来更新标签(这绝对不是一个选择!)

What I need to know is, is there a way to make the image referred to in the a tag automatically be the same as that referred to in the img tag, whatever this may be at the time? 我需要知道的是,是否有一种方法可以使a标签中引用的图像与img标签中引用的图像自动相同,无论当时是什么情况? From research so far I believe that jquery / javascript would be the solution here. 从目前的研究来看,我相信jquery / javascript将是这里的解决方案。
I've tried modifying some other javascript code that I've found, but possibly due to my own ineptitude I haven't been able to make it work. 我曾尝试修改我发现的其他一些JavaScript代码,但可能由于我自己的无能,我无法使其正常工作。

If anyone has a solution, it would be greatly appreciated if they could also idiot-proof it for me, I've successfully used javascript that came complete with script tags, but beyond that I've had little success! 如果有人有解决方案,那么如果他们也可以为我提供白痴证明,将不胜感激,我已经成功使用了带有脚本标签的javascript,但除此之外,我收效甚微!

Thanks for your time reading and for any help anyone can offer! 感谢您的宝贵时间阅读,以及任何人都能提供的帮助!

I am still confused by this question, but i think that's what you want. 我仍然对这个问题感到困惑,但是我想这就是你想要的。 The img src attribute can change from the CMS but no the a . 可以从CMS更改img src属性,但不能更改a Try this to fix it : 尝试解决此问题:

$(document).ready(function(){
    var $a = $('a[rel=lightbox]');
    $a.each(function(){
        $(this).attr('href', $(this).find('img').attr('src'));
    })

})

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

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