简体   繁体   English

专注于AJAX通话后失去的div

[英]Focus on div lost after AJAX call

I seem to be facing a strange issue here (at least seems so). 我似乎在这里面临一个奇怪的问题(至少看起来是这样)。

I have a DIV which has a few links (anchors) in it. 我有一个DIV,其中有一些链接(锚)。 Clicking a link makes an AJAX call (using jQuery), and the server (Controller) sends an HTML response (in fact, a JSP, via Spring MVC). 单击链接进行AJAX调用(使用jQuery),服务器(控制器)发送HTML响应(实际上是通过Spring MVC的JSP)。

Now, this DIV can be reached by scrolling down the page. 现在,可以通过向下滚动页面来达到此DIV。 However, when I click the link within the DIV, I am navigated to the top of the page (the AJAX call doing its job in the background), whereas I expect (WANT) to stay where I was on the page (ie, on that DIV). 但是,当我单击DIV中的链接时,我导航到页面顶部(AJAX调用在后台执行其工作),而我希望(WANT)停留在页面上的原位置(即DIV)。

How can I control that? 我该如何控制?

Make sure the href on your links call void(0) and use the onClick attribute for your AJAX function. 确保链接上的href调用void(0),并将onClick属性用于AJAX函数。

<a href="javascript:void(0);" onClick="myFunction();">click me</a>

Not having a value for the HREF attribute causes the page to scroll to the top. 没有HREF属性的值会导致页面滚动到顶部。 Anchors arent necessarily links to other pages, they can link to areas within the page, so if the HREF is blank it will simply go to the top. 锚定点必然链接到其他页面,它们可以链接到页面内的区域,因此,如果HREF为空白,它将直接转到顶部。

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

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