【问题标题】:Determine which the link is hashtag确定哪个链接是标签
【发布时间】:2011-01-22 03:30:49
【问题描述】:

我是使用 jQuery 的初学者,遇到一个问题。

我制作了一个画廊,并添加了画廊链接以方便标签用户。例如,如果您添加一个收藏链接,访问该页面将看到正确的图像,因为该链接中存在哈希标签。

图库几乎可以工作了,实际上我只是想添加一个类来制作选定的图像集。

$gallery = window.location.hash;

//If have no hashtag, adds the class on the first image
if( $gallery == '')
    jQuery('.gallery-nav ul li').children('a').first().addClass('current');
else
{
    jQuery('.gallery-nav ul li').each(function($i){
        $link_hash = jQuery(this).find('a').attr('href');

        if( $link_hash == $gallery)
        {
            alert ( $link_hash );
        }
    });
}

直到第一部分,“如果没有标签,在第一张图片上添加类”工作正常,但后来我不知道如何。例如,如果链接是:http://example.net/gallery.php#3,我想在第三个链接上添加类。

在上述函数中,它正确显示了警报。当它到达选定的链接时,它会显示警报。但我不知道如何在链接中添加类。

谁能帮帮我? 谢谢。

【问题讨论】:

  • 仅供参考 - 对于此类涉及 DOM 选择的问题,最好提供正在选择的 HTML 示例。

标签: jquery gallery hashtag


【解决方案1】:

您可以直接使用attribute-ends-with-selector(docs) 选择该元素,以选择href$gallery 值结尾的元素。 (我假设少于十个。)

如您所见,$gallery 被连接到选择器字符串中。

else {
    jQuery('.gallery-nav ul li a[href$=' + $gallery + ']').addClass('current');
}

【讨论】:

    猜你喜欢
    • 2017-06-23
    • 2014-03-23
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多