我们许多人在前 MVC 世界中使用的页内锚点肯定会导致 MVC 中的 PITA。我使用了两种不同的修复方法:
- 如果您只是使用'href = #'并且不需要它在任何地方导航,那么使用jquery“e.preventDefault”。如:
$('(Your attribute, class, and/or ID) a).click(function (e) {
//whatever else you might want to do, i.e. highlight,etc...
e.preventDefault(); //Prevent in-page anchor from navigating to root of site
});
(我已经在脚本中看到了足够多的“href='#'”,所以我不再问了,也不再问我为什么了)。
- If you're wanting to navigate somewhere else in the page, add a similar jQuery, but instead of e.preventDefault(), you'll just "return false;". For example, if you have an item
<span><a class="fa fa-arrow-down down-to-bottom" href="#footer"> down</a></span>
that you need to go to page bottom, this will do that and shouldn't throw you out to the 'root':
$('.down-to-bottom').click(function () {
$('html, body').animate({
scrollTop: $(document).height()
}, 1500, 'easeInOutExpo');
return false;
});
Modify it however you want to go to somewhere besides the bottom (like to another "div") --just be sure to "return false;".
Either of these can probably be done in 'vanilla' javascript too. I usually use jQuery because it's easier for me and a lot of others.