gpt4 book ai didi

Javascript帮助,切换文本不断跳回到页面顶部

转载 作者:搜寻专家 更新时间:2023-11-01 05:26:23 25 4
gpt4 key购买 nike

我希望有人能帮助解决这个问题,我已经尝试了很多脚本和解决方法,但无法让这个简单的东西起作用。正如标题所示,我怎样才能让我的文字不跳到页面顶部。这是代码:

<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.

显示会折叠其布局,而可见性会保持其布局。

全部在同一个 html 文件中。我阅读了外人所做的并尝试了

ev.preventDefault();返回错误;

函数末尾的代码,但什么也没有。

最佳答案

A 标记中的 href 属性更改为:javascript:; 而不是 #

所以使用它来创建启用 javascript 的 anchor :

<a href="javascript:;">

添加(背景信息):

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:; or javascript:void(0); to indicate that this is not a navigation anchor.

关于Javascript帮助,切换文本不断跳回到页面顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5004134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com