简体   繁体   English

设置锚标记href滚动到所有URL(例如“ /”,“ / Home”和“ / Home / Index”)中的div的方法,而无需在MVC中进行重定向

[英]What set an anchor tag href to scroll to a div in all URLs like “/” and “/Home” and “/Home/Index” without redirecting in MVC

I need to set HREF attribute of an anchor tag to work in all URLs like "/" and "/Home" and "/Home/Index" to scroll in top of a div. 我需要设置锚标记的HREF属性以在所有URL中工作,例如“ /”,“ / Home”和“ / Home / Index”,以便在div顶部滚动。

the problem is if i set that to "/Home/Index#ToTop" and someone click on that anchor tag, he/she will redirect from "/" or "/Home" to "/Home/Index#ToTop" that is not very nice! 问题是,如果我将其设置为“ / Home / Index#ToTop”,并且有人单击该锚标记,则他/她将从“ /”或“ / Home”重定向到“ / Home / Index#ToTop”,而不是非常好!

any idea? 任何想法?

If you just want to scroll to the top <a href="#"> will bring you back to the top of the page. 如果您只想滚动到顶部, <a href="#">会将您带回到页面顶部。 If you want to scroll to an element that's somewhere on the page use <a href="#id-of-div"> so without the /Home/foo/bar stuff. 如果要滚动到页面上某个位置的元素,请使用<a href="#id-of-div">因此不要使用/ Home / foo / bar内容。

See also: What is href="#" and why is it used? 另请参阅: 什么是href =“#”以及为什么使用它?

Edit based on comment 根据评论进行编辑
So you want the link to scroll to top of home page if you're still on the homepage without redirect, and you want the link to redirect you to home page and scroll to top of it, if you're not on the home page? 因此,如果您仍在主页上而不进行重定向,那么您希望链接滚动到主页顶部;如果您不在主页上,则希望该链接将您重定向到主页并滚动到其顶部。 ? I think you've to create a different link depending on the page you're on. 我认为您必须根据所在页面来创建其他链接。 So if user is yoursite.com/home, the link will be <a href="#id-of-div"> and if user is yoursite.com/foo/bar the link will be <a href="./Home/Index#id-of-div"> 因此,如果用户是yoursite.com/home,则链接将为<a href="#id-of-div"> ;如果用户是yoursite.com/foo/bar,则链接将为<a href="./Home/Index#id-of-div">

You can determine the current URL in the controller. 您可以在控制器中确定当前URL。 Quick search on SO gives me: 快速搜索SO给我:
How to get current page URL in MVC 3 In .NET MVC, is there an easy way to check if I'm on the home page? 如何在MVC 3中获取当前页面的URL。 在.NET MVC中,是否有一种简单的方法来检查我是否在主页上?

It's been a while since I've worked with MVC, so I can only help with some old memories and quick searching 自从我与MVC合作以来已经有一段时间了,所以我只能帮您解决一些旧的回忆和快速搜索

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM