如何通过HTML标记或JS代码实现跳转返回页面顶部

JavaScript08

如何通过HTML标记或JS代码实现跳转返回页面顶部,第1张

可以通过html的锚标签来实现

<html>

<head></head>

<body>

<a id="top"></a>

.........................

<!--在返回顶部按钮处写-->

<a href="#top">返回顶部</a>

</body>

</html>

js的写法

页面上的返回顶部按钮

<button type="button" onclick="GoTop()"></button>

js中的写法

function GoTop(){

if (document.body &&document.body.scrollTop &&document.body.scrollLeft)

{

document.body.scrollTop=0

}

if (document.documentElement &&document.documentElement.scrollTop &&document.documentElement.scrollLeft)

{

document.documentElement.scrollTop=0

}

}

设计一个按键

$(".scroll-top").click(function () {

  $(window).scrollTop(0)

})

或者

event.preventDefault()

因为浏览器会默认链接是要去往某个页面没有href就直接刷新当前页面了