简体   繁体   English

从外部链接到页面携带变量并插入jQuery脚本onload

[英]Carrying Variable from external link to page and plugging into jQuery script onload

I have this jQuery which shows/hides content depending on the list item that's clicked. 我有这个jQuery,它根据单击的列表项显示/隐藏内容。 I also want to be able to show specific content upon landing on this page, dependant on what link the user clicked on another page. 我还希望能够在登陆该页面时显示特定的内容,具体取决于用户在另一页面上单击的链接。

I know that to make this happen, I have to carry the variable over from the other link somehow, and make it act as the variable. 我知道要实现这一点,我必须以某种方式将变量从其他链接中继承下来,并使其充当变量。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>    
<script type="text/javascript"> 
$(document).ready(function() {
$('#side_nav ul li a').click(function() {
    $('#side_nav ul li.current').removeClass('current');
    $(this).parent().addClass('current');

    var filterVal = $(this).attr('class');

        $('#content div.section').each(function() {
            if($(this).hasClass(filterVal)) {
                $(this).fadeIn(200);
            } else {
                $(this).hide();
            }
        });
    return false;
});
});
</script>

I've tried adding #desired_content to the end of the url at the link on the separate page and I know that using window.location.hash, I can pull that hash lable in as the variable somehow but I'm lost as to exactly how to accomplish that. 我试过在单独页面上的链接末尾的网址末尾添加#desired_content,我知道使用window.location.hash可以以某种方式将哈希标签作为变量拉进去,但是我却迷失了如何做到这一点。

Please help. 请帮忙。

EDIT: I added this to the external page: 编辑:我将此添加到外部页面:

<li><a href="./about.html#how_it_works">HOW IT WORKS</a></li>

and this to the target page, just to TEST whether the class being added 然后将其添加到目标页面,以测试是否添加了类

<script type="text/javascript"> 
$(document).ready(function() {
    var myHash = window.location.hash;

    if( myHash == "#how_it_works" ){
        $('#content div.how_it_works').addClass('test');
    }else if( myHash == "#option2" ){
        // fade in option2
    }


});     
</script>

I don't understand why this isn't working... 我不明白为什么这行不通...

Hash URLs have mostly been used for in-page anchor tags (back to top, etc) and so I'd suggest that directing someone to a certain section of your page using hash URLs makes a lot of sense. 哈希URL主要用于页面内定位标记(从顶部到顶部等),因此,我建议使用哈希URL将某人定向到页面的特定部分非常有意义。

What I've done, once on the page, was grab the hash URL via window.location.hash and manipulate your page based on this string. 一旦在页面上,我所做的就是通过window.location.hash获取哈希URL并基于此字符串来操纵页面。

var myHash = window.location.hash;

if( myHash == "#desired_content" ){
// fade in option1
}else if( myHash == "#option2" ){
// fade in option2
}

I will suggest to store the variable on localStorage in first page then load the variable from localStorage in second page. 我建议将变量存储在第一页的localStorage上,然后从第二页的localStorage加载变量。 It will be simpler rather than passing hash URL which is not the proper purpose. 这比传递不正确的哈希URL更为简单。

First Page 第一页

<a href="secondPage.html" id="link">Go to second page</a>
<script type="text/javascript"> 
$(document).ready(function() {
$('#link').click(function(){
    // set your variable when someone click the link
    localStorage.setItem("myVariable", "someValue");
});
});
</script>

Second Page 第二页

<script type="text/javascript"> 
$(document).ready(function() {
var yourVariable = localStorage.getItem("myVariable");
// do your stuff according to this variable that passed from firstpage
//...
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM