简体   繁体   English

单击链接时显示图像

[英]When Click Link Display Image

I want an image to get displayed on a page when the user clicks on a certain link that looks like #foo. 我希望当用户单击某个看起来像#foo的链接时在页面上显示图像。 Example

<a href="#dog">Dog</a>
<a href="#cat">Cat</a>

If the url contains #dog it will display a picture of a dog. 如果网址中包含#dog,它将显示一条狗的图片。 if the url contains #cat it will display a picture of a cat. 如果网址包含#cat,它将显示一只猫的图片。 How can this be achieved. 如何做到这一点。 Then have the images be displayed in the image tag. 然后将图像显示在图像标签中。

<img id="something" />

Edit: I'm thinking of maybe having all images hidden until link id clicked. 编辑:我正在考虑隐藏所有图像,直到单击链接ID。 Then somewho get the url and determine which image to display. 然后有人获得该URL并确定要显示的图像。 The most important part is to do it from identifying the URL 最重要的部分是通过识别URL来实现

A sudo code idea of what I'm trying to achieve. 我想要实现的sudo代码构想。

    if url = #dog then 
    display in idelement dog.jpg 
    else if url = #cat then  
    display in idelement cat.jpg 
    else display defualt.jpg


<img id="idelement" />

i made an example here but it miss how you would like to display the image : http://jsfiddle.net/VgkUL/3/ 我在这里举了一个例子,但它想念您如何显示图像: http : //jsfiddle.net/VgkUL/3/

<a href="#foo" >#foo....</a>
<a href="#cat" >#cat....</a>

$('a').each( function() {
    $(this).click( function(e) {
        var imgUrl = $(this).attr("href"); // url of the image, you can save it somewhere else into the a Tag
        alert(imgUrl);
        // display here the image where you want
        e.preventDefault();
        return false;
    })
})

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

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