admin管理员组文章数量:1332361
I hope someone can help with this issue, I've tried many scripts and workarounds, but can't get this simple thing to work. As the title has it, how can I make my text NOT jump to the top of the page. Here is the code:
<script type="text/javascript">
function toggleVisibility() {
document.getElementById("toggleMe").style.display = "";
if(document.getElementById("toggleMe").style.visibility == "hidden" ) {
document.getElementById("toggleMe").style.visibility = "visible";
}
else {
document.getElementById("toggleMe").style.visibility = "hidden";
ev.preventDefault();
return false;
}
}
</script>
<a href="#" onclick="toggleVisibility();">Click to toggle visibility.</a></p>
<div id="toggleMe" style="visibility: hidden;"> Something to Hide and show.
Display collapses it's layout while visibility will keep it's layout.
All in the same html file. I read what outhers did and I tried the
ev.preventDefault(); return false;
code at the end of the function, but nothing.
I hope someone can help with this issue, I've tried many scripts and workarounds, but can't get this simple thing to work. As the title has it, how can I make my text NOT jump to the top of the page. Here is the code:
<script type="text/javascript">
function toggleVisibility() {
document.getElementById("toggleMe").style.display = "";
if(document.getElementById("toggleMe").style.visibility == "hidden" ) {
document.getElementById("toggleMe").style.visibility = "visible";
}
else {
document.getElementById("toggleMe").style.visibility = "hidden";
ev.preventDefault();
return false;
}
}
</script>
<a href="#" onclick="toggleVisibility();">Click to toggle visibility.</a></p>
<div id="toggleMe" style="visibility: hidden;"> Something to Hide and show.
Display collapses it's layout while visibility will keep it's layout.
All in the same html file. I read what outhers did and I tried the
ev.preventDefault(); return false;
code at the end of the function, but nothing.
Share Improve this question edited Dec 2, 2020 at 13:21 peterh 1 asked Feb 15, 2011 at 13:19 OllieOllie 512 silver badges6 bronze badges 1- you don't need ev.preventDefault(); return false; also depending on the rest of your markup there could be some nuances, for example missing closing tags, other improper html, etc. – Kris Ivanov Commented Feb 15, 2011 at 13:23
2 Answers
Reset to default 9Change the href
attribute in the A
tag to: javascript:;
instead of #
So use this to create your javascript enabled anchor:
<a href="javascript:;">
added (background info):
The
#
character indicates a bookmark. By omitting the name of the bookmark e.g.#bottom
the browser considers the bookmark as 'top of the page' thus scrolling to the top. Your problem does not have to do anything with bookmarks, however using#
to define an empty link for a javascript enabling anchor (A
) tags is not the way to go. You should use:javascript:;
orjavascript:void(0);
to indicate that this is not a navigation anchor.
Or simply always return false, not just in the Else clause.
本文标签: Javascript helptoggle text keeps jumping back to top of pageStack Overflow
版权声明:本文标题:Javascript help, toggle text keeps jumping back to top of page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742318421a2452286.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论