html / javascript:使用变量作为< a href>的url [英] html/javascript: using a variable as url for <a href>

查看:591
本文介绍了html / javascript:使用变量作为< a href>的url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个带有一点javascript的html页面。
我有几个标签,点击一个链接后,其中一个标签会动态分配一个背景图片。这个URL存储在一个全局变量中。到目前为止很好。

I developed a html page with a bit of javascript. I have several tags and after clicking a link one of them is dynamically assigned a background image. This url is stored in a global variable. So far so good.

var picture,thumb

function changeDivs(p)
{
     if (p!='') {
        picture = './images/p'+p+'.jpg'; // global url variable
        thumb = './thumbs/p'+p+'.jpg'; // global url variable
        var pic = "url("+thumb+")";
        document.getElementById("poster").style.backgroundImage = pic;
        }
}

我像这样调用函数:

<a href="javascript:changeDivs('2')">Link</a>

现在我想使用需要像这样访问的thumbnailviewer脚本:

Now I want to make use of a thumbnailviewer script which needs to be accessed like this:

<a href="javascript: global variable" rel="thumbnail"> <!-- Of course this "javascript: global variable" should be a valid url: the stored 'picture' variable -->
        <div id="poster"> 
        </div>
    </a>

所以我把url存储在变量中:picture

So I have stored the url in variable: picture

如何通过< a href> 访问thumbnailviewer脚本并使用该变量。我不知道是否可以通过函数或如何调用缩略图脚本。

How can I access the thumbnailviewer script through <a href> AND use the variable. I don't know if the thumbnail script can be called through a function or how.

非常感谢!

推荐答案

我认为这是你正试图实现的目标。这里是 jsfiddle
'< ;点击一个href>'图片变为< div id ='poster'>

I think this is what you are attempting to achieve. Here is the jsfiddle.
When an '<a href>' is clicked an image becomes the background of "<div id='poster'>"

我用您现有的缩略图网址替换了您的图片网址。请确保您的图片和缩略图变量的网址存在。此外,我设置了您的海报div的宽度和高度。

I replaced your pic's URL with your existing thumbnail url. Please make sure your picture and thumb variable's URL exist. Also, i set width and height of your poster's div.

要使用thumbnailviewer,请尝试使用新的jsfiddle链接。仅供参考,dynamicdrive阻止jsfiddle访问js文件。请尝试在您自己的项目上使用代码。

To work with thumbnailviewer, try the new jsfiddle link please. FYI, dynamicdrive blocked jsfiddle from accessing the js files. Please try the code on your own project.

这篇关于html / javascript:使用变量作为&lt; a href&gt;的url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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