确定哪个链接是主题标签
我是 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,我想在第三个链接上添加类。
在上面的函数中,它正确地显示了警报。当它到达选定的链接时,它会显示警报。但我不知道如何在链接中添加类。
有人可以帮助我吗? 谢谢。
I'm a beginner with using jQuery and am experiencing a question.
I made a gallery and the gallery links are added to facilitate the hashtags user. For example, if you add a favorite link, to access the page will see the right image, because the hash tag is present in the link.
The gallery is almost working, actually I just wanted to add a class to make the selected image set.
$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 );
}
});
}
Until the first part, "If have no hashtag, adds the class on the first image" is working right, but then I do not know how. For example, if the the link is: http://example.net/gallery.php#3, I want to add the class on the third link.
In the above function, it shows the alert correctly. When it reaches the selected link it shows the alert. But I do not know how to add the class in the link.
Could anyone help me?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
attribute-ends-with-selector< 直接选择该元素/code>(docs)
选择
href
以$gallery. (我假设少于十个。)
如您所见,
$gallery
连接到选择器字符串中。You could just select that element directly using the
attribute-ends-with-selector
(docs) to choose the element where thehref
ends with the value of$gallery
. (I assume there are fewer than ten.)As you can see, the
$gallery
is concatenated into the selector string.