HTML 根据当前页面,一个 href 链接到两个不同的页面

HTML one href linking to two different pages depending on current page

我有一个静态菜单,在所有页面视图中都相同。在主页上,它应该 link 到页面底部(#footer),当你转到子页面时,它会 link 到另一个页面。 在我的例子中,我在 Magento 商店上有一个博客,在首页的底部有一些博客文章,当点击主菜单时,它应该重定向到那里。但是当用户转到类别或产品页面时,它应该 link 直接写博客。

我不确定这是否可行,google 也没有帮助。 如果有任何方法,我很乐意看到它们。 谢谢!

你的解决方案是这样的:

<a href="#" id="myBtn">Click Me</a> 

<script>

$(document).ready(function(){

    $('#myBtn').click(function(){

        if(window.location.href == "http:// ...."){ //used to find the current page location 
            location.href = "#abc"; //at the bottom of page
        }
        else{
            location.href = "http:// .... ";//the next page you want to take the user to
        }

    });

});

</script> 

使用普通 html,任何锚点不能有多个 href 位置。但是,通过使用 JavaScript(我使用过 jQuery Lib),您可以首先找到当前网页位置,并基于它重定向用户点击超链接或按钮到您想要的其他位置。

也许您可以使用 Javascript 获取您所在的页面。然后构建一个 if 语句,其中包含类似 if (page == homepage) 的内容以转到页脚和(if page == category 转到产品)。

如果子页面在不同的 URL,通过检查当前 URL 使用条件。

Get current URL in web browser

   <script>

function myFunction() {

var str = window.location;

var patt = new RegExp("#ide");

var res = patt.test(str);

if(res){

  location.replace("http://www.homepage.com")

}else{

  location.replace("http://www.subpage.com#ide")

}

}

也许这个脚本会对你有所帮助。