替换图片src和href onclick [英] Replace image src and a href onclick

查看:45
本文介绍了替换图片src和href onclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小项目,我想在其中交换图像,并在灯箱中显示新图像.

I've got this little project where I want to swap an image for another, and display the new image in a lightbox.

以下代码的工作方式如下;当我单击其中一个小图像时,大图像的图像src被替换为小图像的src.但是,当我单击大图像时,它仍会在灯箱中打开默认的大图像.

The code below works as follows; when I click one of the small images, the image src of the large image is replaced with the src of the small image. However, when I click the large image, it still opens the default large image in my lightbox.

是否有什么方法可以使超链接href交换,就像交换图像src一样?他们需要获得相同的值.

Is there any way I can make the hyperlink href get swapped the same way the image src is swapped? They need to get the same value.

javascript:

The javascript:

<script type="text/javascript">
    function switch1(div) {
        if (document.getElementById('one')) {
            var option=['one','two','three'];
            for(var i=0; i<option.length; i++) {
                obj=document.getElementById(option[i]);
                obj.style.display=(option[i]==div)? "block" : "none";
            }
        }
    }
    function switchImg(i){
        document.images["main-image"].src = i;
    }
</script>

HTML/PHP:

<?php
    $image = 'main-image.jpg';
    $small_1 = 'small-image-1.jpg';
    $small_2 = 'small-image-2.jpg';
?>
<div id="holder">
<div class="large">
    <a href="<?php echo $image; ?>" rel="lightbox">
        <img id="main-image" src="<?php echo $image; ?>" />
    </a>
    <a class="original" onclick="switchImg('<?php echo $image; ?>')">Back to original image</a>
</div>
<div class="small">
    <a onclick="switchImg('<?php echo $small_1; ?>')">
        <img src="<?php echo $small_1; ?>" />
    </a>
    <a onclick="switchImg('<?php echo $small_2; ?>')">
        <img src="<?php echo $small_2; ?>" />
    </a>
</div>
</div>

推荐答案

更改href:

document.getElementById('YOUR_ID').href = 'abraCaDabraDotCom';

这篇关于替换图片src和href onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆