检查URL是否包含已经点击的链接的href [英] Check if URL contains href of link I already clicked

查看:84
本文介绍了检查URL是否包含已经点击的链接的href的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在列表中,我有几个链接:

In a list I have a few links:

 <ul class="dropdowner" id="coll-filter">
    <li><a href="#black">Black</a></li>
    <li><a href="#white">White</a></li>
    <li><a href="#blue">Blue</a></li>
</ul>

另一个输出我使用了+而不是#在url Ie:

Another output I have uses + instead of # in the url Ie:

  <ul class="dropdowner" id="coll-filter">
    <li><a href="+black">Black</a></li>
    <li><a href="+white">White</a></li>
    <li><a href="+blue">Blue</a></li>
</ul>

如果我点击链接白色,那么#white被插入到我的URL中。 (mydomain.com/#white)
我想避免重复,所以有一种方法来检查URL中是否已经存在#white,如果是,则不允许链接被点击。

If I click the link White then "#white" is inserted into my URL. (mydomain.com/#white) I want to avoid duplicates so is there a way to check if "#white" already exist in URL and if so don't allow the link to be clicked.

推荐答案

以下将禁用与#或+之后的URL组件匹配的链接,并启用任何以前禁用的链接: / p>

The following will both disable a link which matches the component of the URL after the # or + as well an enable any previously disabled links:

$('.dropdowner a').on('click', function() {
    var url = window.location,
        tag = url.split('+'),
        hash = window.location.hash,
        supress = function(term) {
            $('.dropdowner a[href=' + term + ']').bind('click', false);
        };      
    $('.dropdowner a').unbind('click', false);
    if (hash) {            
        supress(hash);
    } else if (typeof tag !== 'undefined') {            
        supress('+' + tag);
    }        
});


请参阅: br>

  • http://api.jquery.com/bind/
  • http://api.jquery.com/unbind/
  • https://developer.mozilla.org/en-US/docs/DOM/window.location#Properties

这篇关于检查URL是否包含已经点击的链接的href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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