简体   繁体   English

<a>单击</a>空<a>标签</a>时,停止页面从顶部跳回顶部

[英]stop page from jumping back to top when empty <a> tag is clicked

The title says it all, how would I make it using only html, no JAVASCRIPT, to stop the page from jumping back to the top if a user clicks on an empty tag? 标题说明了这一点,如果用户点击空标签,如何仅使用html,没有JAVASCRIPT来阻止页面跳回到顶部? So for example, if at the very bottom of my site, I have a link that is empty, but click on it, it takes be clear back up to the top... 例如,如果在我的网站的最底部,我有一个空的链接,但点击它,需要清楚回到顶部...

A simple solution to this would simply put in the a tag: 一个简单的解决方案是将其放在a标签中:

<a href="#a">Title</a>

In doing this, it won't scroll your page back to the top. 这样,它不会将页面滚动回到顶部。 To have it scroll back to the top, take out the a after the # symbol...so it would look like this: 要使它滚动回到顶部,请在#符号后面取出a ...,这样看起来像这样:

<a href="#">Title</a>

That is the best explanation I can give you without any code provided from you. 这是我可以给你的最好的解释,没有你提供任何代码。

Give that a try and it should work with what you are asking for...No javascript is needed. 试一试,它应该与你要求的一起工作......不需要javascript。 In fact you can even make the #a jump to a different location if you'd like on your page :) 事实上,如果您愿意在页面上,您甚至可以将#a跳转到其他位置:)

UPDATE: 更新:

This may suit you better! 这可能更适合你! Add this to either a js file or add it inline with you html document. 将其添加到js文件或将其与您的html文档内联添加。

Separate js file (just make sure to call it externally on your html file): 单独的js文件(只需确保在html文件外部调用它):

$('#Add_Your_Id_Or_Class_Here').removeAttr('href');

Example: $('#link a').removeAttr('href'); 示例: $('#link a').removeAttr('href'); or $('.link a').removeAttr('href'); $('.link a').removeAttr('href'); or even $('a').removeAttr('href'); 甚至是$('a').removeAttr('href');

Now, if you want to achieve this via inline on your html file, simply do this: 现在,如果你想通过你的html文件内联实现这一点,只需这样做:

<script>
    $('#Add_Your_Id_Or_Class_Here').removeAttr('href');
</script>

Again, you can use any of the examples above as well. 同样,您也可以使用上面的任何示例。 In fact there are many ways you can achieve this now that I think about it...Hope that helps :) 事实上,现在有很多方法可以实现这一点,我想一想......希望有帮助:)

如果您的链接不应该链接(例如,当它只是链接所在的占位符时),那么您不应该为<a>元素添加[href]属性

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

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