Javascript 将变量从外部链接携带到页面并插入jQuery脚本onload
我有一个jQuery,它根据单击的列表项显示/隐藏内容。我还希望能够显示特定的内容登陆到这个页面,这取决于什么链接的用户点击了另一个页面 我知道要做到这一点,我必须以某种方式将变量从另一个链接中带过来,并使其充当变量Javascript 将变量从外部链接携带到页面并插入jQuery脚本onload,javascript,jquery,variables,show-hide,Javascript,Jquery,Variables,Show Hide,我有一个jQuery,它根据单击的列表项显示/隐藏内容。我还希望能够显示特定的内容登陆到这个页面,这取决于什么链接的用户点击了另一个页面 我知道要做到这一点,我必须以某种方式将变量从另一个链接中带过来,并使其充当变量 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script> <script type=
<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>
$(文档).ready(函数(){
$('side#nav ul li a')。单击(函数(){
$('side#nav ul li.current').removeClass('current');
$(this.parent().addClass('current');
var filterVal=$(this.attr('class');
$('#content div.section')。每个(函数(){
if($(this).hasClass(filterVal)){
美元(本).fadeIn(200);
}否则{
$(this.hide();
}
});
返回false;
});
});
我曾尝试在单独页面的链接处将#所需#内容添加到url的末尾,我知道使用window.location.hash,我可以以某种方式将该哈希标签作为变量拉入,但我不知道具体如何实现
请帮忙
编辑:我将其添加到外部页面:
<li><a href="./about.html#how_it_works">HOW IT WORKS</a></li>
并将其添加到目标页面,只是为了测试是否要添加类
<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>
$(文档).ready(函数(){
var myHash=window.location.hash;
if(myHash==“#它是如何工作的”){
$('content div.how_it_works').addClass('test');
}else if(myHash==“#选项2”){
//淡入选项2
}
});
我不明白为什么这不起作用…我建议将变量存储在第一页的localStorage上,然后从第二页的localStorage加载变量。它比传递散列URL更简单,因为散列URL不是正确的用途 首页
<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>
$(文档).ready(函数(){
$(“#链接”)。单击(函数(){
//当有人单击链接时设置变量
setItem(“myVariable”、“someValue”);
});
});
第二页
<script type="text/javascript">
$(document).ready(function() {
var yourVariable = localStorage.getItem("myVariable");
// do your stuff according to this variable that passed from firstpage
//...
});
</script>
$(文档).ready(函数(){
var yourVariable=localStorage.getItem(“myVariable”);
//根据从firstpage传递的这个变量进行操作
//...
});
散列URL主要用于页面内锚定标记(返回顶部等),因此我建议使用散列URL将某人引导到页面的某个部分非常有意义
一旦进入页面,我所做的就是通过window.location.hash获取散列URL,并基于此字符串操纵页面
var myHash = window.location.hash;
if( myHash == "#desired_content" ){
// fade in option1
}else if( myHash == "#option2" ){
// fade in option2
}
您能给我一个示例,说明如何在这里使用window.location.hash命令吗。(它实际上不是页面上的锚,而是一个变量,需要在页面加载时通过脚本运行该变量,以确保显示正确的内容)您能看看我上面的编辑吗?不知道我做错了什么。