通过跟随URL将类添加到其他页面上的div

问题描述 投票:0回答:1

我的主页上有一些锚链接。在另一页上,我有一些切换部分。如果我单击主页上的某个项目,则将尝试该链接,然后将一个类添加到该页面上的特定div,以实现功能。这是我尝试但不起作用的内容

     $("#home_pro a").each(function(){
        var ThisHref = ($(this).attr('href').split('?'))[0];
        if(window.location.href == ThisHref) {
            $('.item1 .elementor-tab-title, .item1 .elementor-tab-content').addClass('active');
        }
   });
jquery anchor
1个回答
0
投票

您只能将一个类添加到已加载的新页面After中。因此,请在该页面中准备好文档($(function(){})),并使用window.location.search读取url查询(URL中?后面的部分),并相应地添加您的类。或者,您可以发送会话或cookie ...

例如,考虑以下URL:mysite.com/login.php?activated,然后在login.php页面中,您的脚本可能类似于:

$(function(){
  query=window.location.search;
  if (query =='activated'){
     $('.item1 .elementor-tab-title, .item1 .elementor-tab-content').addClass('active');
  }
})
© www.soinside.com 2019 - 2024. All rights reserved.