单击,导航到另一个页面并打开一个由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()
    (如果存在)

jQuery的.click()不带任何参数,在该元素上触发click事件,因此在最简单的解决方案中,如果用户通过单击shoes链接进入产品页面,请在末尾添加查询字符串(/products.php/?q=shoes)

然后在产品页面的javascript中,如果它看到需要一个产品,它可以自动触发该页面上任何你应该点击的元素的点击事件,使其显示出来

这个问题有一个如何使用jQuery从URL中提取参数的示例

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阅读

index.html中的链接

<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');
  }
});