单击“链接显示图像”时

时间:2013-08-11 23:55:42

标签: javascript jquery

当用户点击看起来像#foo的某个链接时,我希望在页面上显示图像。实施例

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

如果网址包含#dog,则会显示狗的图片。如果网址包含#cat,则会显示一张猫的图片。如何实现这一目标。然后让图像显示在图像标签中。

<img id="something" />

编辑:我想在点击链接ID之前隐藏所有图片。然后一些人获取网址并确定要显示的图像。最重要的部分是通过识别URL

来实现

关于我想要实现的目标的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" />

1 个答案:

答案 0 :(得分:0)

我在这里做了一个例子,但是它想念您希望如何显示图像: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;
    })
})
相关问题