单击,导航到另一个页面并打开一个由javascript隐藏的div
我有两页。让我们调用第一页单击,导航到另一个页面并打开一个由javascript隐藏的div,javascript,jquery,Javascript,Jquery,我有两页。让我们调用第一页index.html和第二页products.html 在products.html上,我有一个隐藏的div,除非用户单击按钮将其显示,如下所示: products.html $(文档).ready(函数(){ $('.product highlight').hide(); $('a[href$=shoes')。单击(函数(){ $('鞋').show(); }); }); 这些是鞋子 您可以使用伪类。为此,请定义下一个CSS规则: #鞋{ 显示:无;/*默认情况下
index.html
和第二页products.html
在products.html
上,我有一个隐藏的div,除非用户单击按钮将其显示,如下所示:
products.html
$(文档).ready(函数(){
$('.product highlight').hide();
$('a[href$=shoes')。单击(函数(){
$('鞋').show();
});
});
这些是鞋子
您可以使用伪类。为此,请定义下一个CSS规则:
#鞋{
显示:无;/*默认情况下隐藏*/
}
#shoes:target、/*和show,如果存在类显示(单击)*/
#shoes.show{/*或位置哈希匹配id“shoes”*/
显示:块;
}
在JS中,您将添加类show
:
$(document).ready(function() {
$('.product-highlight').hide();
$('a[href$=shoes').click(function() {
$('#shoes').addClass('show');
});
});
从索引页重定向时,您还需要设置一个散列#shoes
:
$(document).ready(function() {
$('a[href$=shoes]').click(function() {
window.location.href= 'http://sample.com/products.php/#shoes';
});
});
一个战略:
- 将index.html链接到(普通的
就可以了,这里不需要jQuery单击事件。) - 让products.php检查document.location.hash中的“#shoes”并触发
(如果存在)$(“#shoes”).show()
function getUrlParameter(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}
函数getUrlParameter(sParam)
{
var sPageURL=window.location.search.substring(1);
var sURLVariables=sPageURL.split('&');
对于(变量i=0;i<a href="products.html#shoes">Take me to the shoes</a>
当您在products.html中有一个
位置.hash
目标到一个名为#shoes
的元素时,脚本将触发事件按钮“单击”
,以显示您的精彩鞋。添加一行代码,如下所示:
$(文档).ready(函数(){
$('.product highlight').hide();
$('a[href$=shoes')。单击(函数(){
$('鞋').show();
});
//添加此行。。。
$(window.location.hash.show();
});
这些是鞋子
你的问题是什么?你在问如何在页面加载时运行一些代码?看起来你已经知道如何运行了,所以我不确定问题出在哪里。
$(document).ready(function() {
$('.product-highlight').hide();
$('a[href$=shoes]').click(function() {
$('#shoes').show();
});
if ( location.hash != 0 && location.hash == '#shoes' ){
$('a[href$=shoes]').trigger('click');
}
});