【问题标题】:jQuery - add active to link if url is pasted to explorer windowjQuery - 如果将 url 粘贴到资源管理器窗口,则将活动添加到链接
【发布时间】:2012-02-25 06:57:50
【问题描述】:

我在 jQuery 中有这段代码:

$(document).ready(function() {  

        // sprawdzanie wartości url
        var hash = window.location.hash.substr(1);  
        var href = $('.seeker .left ul li a').each(function(){  
            var href = $(this).attr('href');  
            if(hash==href.substr(4,href.length-8)){  
                var toLoad = hash+'.htp';  
                $('#loader').load(toLoad)  
            }  
        });  

        $('.seeker .left ul li a').click(function(){  

        var toLoad = $(this).attr('href');  
        $('#loader').hide('fast',loadContent);  
        $('#load').remove();  
        $('#loader').append('<span id="load"><img src="img/loader.gif" alt="Ładuję..."</span>');  
        $('#load').fadeIn('fast');
        window.location.hash = $(this).attr('href').substr(4,$(this).attr('href').length-8);  
        function loadContent() {  
            $('#loader').load(toLoad,'',showNewContent())  
        }  
        function showNewContent() {  
            $('#loader').show('fast',hideLoader());  
        }  
        function hideLoader() {  
            $('#load').fadeOut('fast');  
        }  
        return false;  

        });

$(function() {

//dodawanie klasy aktywnej do kliknietego linku
    $('.seeker .left ul li a').click(function() {
         $('.seeker .left ul li a').removeClass('active');
         $(this).addClass('active');

        return true;
    }).filter(':first').click();
   });

});

并且此代码正在重新加载 div 内容而不重新加载整个页面并将活动类添加到单击的链接。 我的问题是如果我从浏览器复制链接粘贴到新窗口,如何添加活动类并正确加载内容?

【问题讨论】:

    标签: jquery url hyperlink load paste


    【解决方案1】:

    编辑:您可以使用内置的 js window.location.hash 来获取 url 中的哈希值,然后执行您的逻辑。

    // get value of hash
    var hashVal = window.location.hash;
    
    if (hashVal != null)
    {
      // add active class
      $("'" + hashVal + "'").addClass('active');
      // do something else...
    }
    

    hashVal 将返回包含的哈希值,即:“#hashVal”

    【讨论】:

    • 如果链接是mysite.com/#sth,它会调用锚点“inc/sth.htp”,如果我将此链接粘贴到浏览器,或者我会将此链接发送给我想添加活动类的人这个锚点并加载子站点的内容。
    • 我不明白你在说什么 :) 抱歉,我在 jQ 和 js 中太蹩脚了
    猜你喜欢
    • 2018-06-17
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多