Colorbox查询字符串href,添加动态ID?

时间:2011-04-06 00:15:15

标签: jquery url hash href colorbox

我已编码颜色框,以便我可以使用网址http://example.com?open=true在颜色框中打开页面上的第一个图像。我想使用附加到颜色框链接的ID添加标记,以便在颜色框中打开特定图像(而不仅仅是页面上的第一个图像)。网址应如下所示:http://example.com?id=12345?open=true

我知道这对你们大多数人来说可能是小菜一碟,所以请帮助我,我正在努力学习。

这是我目前使用的彩盒代码:

$(document).ready(function(){
    var
    vars = [],
    hash,
    hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
    }
    $("a[rel='example1']").colorbox({open:vars['open'] == 'true' ? true : false});}); 

我完全不了解jquery,但我相当确定有一种简单的方法可以在自动打开的URL中添加ID吗?链接ID是通过Wordpress自动提供的,所以我无法手动将每个id输入到jquery中。

以下是图片链接的代码:

<a href="/images/exampleimage.jpg" rel="example1" id="<?php the_ID(); ?>">
<img src="images/exampleimagethumbnail.jpg" border="0"></a>

1 个答案:

答案 0 :(得分:0)

你在vars ['id']中有id,所以为什么不这样做:

 $("a[id="+vars['id']+"]").colorbox({open:vars['open'] == 'true' ? true : false})