简体   繁体   English

JavaScript-点击链接后,页面返回顶部

[英]JavaScript - Page goes back to the top after clicking on link

This makes it anoying because you have to scroll back down to where you were. 这很麻烦,因为您必须向下滚动到原来的位置。 Can someone tell me what this behavior is. 有人可以告诉我这种行为是什么。

function toggle_visibility(id) {
   var e = document.getElementById(id);
   if(e.style.display == 'block')
      e.style.display = 'none';
   else
      e.style.display = 'block';
}

Html HTML

<a href="#" onclick="toggle_visibility('foo');">School Area</a>
<div id="foo" style="display: none;">...</div>

This happens because href="#" tells the browser to jump to the top of the page when the link is clicked. 发生这种情况是因为href="#"告诉浏览器单击链接时跳至页面顶部。

You can prevent that default action by returning false from your onclick attribute handler: 您可以通过从onclick属性处理程序返回false来阻止该默认操作:

<a href="#" onclick="toggle_visibility('foo');return false;">School Area</a>

But as you're using jQuery, consider attaching that event handler dynamically (or even using event delegation), and then use preventDefault : 但是,当您使用jQuery时,请考虑动态附加该事件处理程序(甚至使用事件委托),然后使用preventDefault

<a href="#" class="toggle-vis" data-visibility="foo">School Area</a>

then here's a delegated handler: 然后是一个委托处理程序:

$(document.body).on("click", ".toggle-vis", function(e) {
    e.preventDefault();
    toggle_visibility(this.getAttribute("data-visibility"));
});

note how the data-visibility attribute controls what we toggle. 请注意data-visibility属性如何控制我们切换的内容。


You'll get people telling you to change this line: 您会看到有人告诉您更改此行:

toggle_visibility(this.getAttribute("data-visibility"));

to

// Only do this if you understand what `data` does
toggle_visibility($(this).data("visibility"));

but data is not just an accessor for data-* elements, it does more (and less) than that. 但是data 不仅data-*元素的访问者,它的作用更多(更少)。 If you don't need the features that work sets up, there's no need to wrap the element in a jQuery instance and call data. 如果您不需要可设置的功能,则无需将元素包装在jQuery实例中并调用data. But if you prefer More jQuery™: 但是,如果您喜欢More jQuery™:

toggle_visibility($(this).attr("data-visibility"));

also works. 也可以。

This happens because of # . 这是由于# Add this to href : 将此添加到href

<a href="javascript:void(0)"></a>

Default behavior of <a> elements is navigation/redirection <a>元素的默认行为是navigation/redirection

Use e.preventDefault() to prevent default action 使用e.preventDefault()阻止默认操作

 function toggle_visibility(e, id) { e.preventDefault(); var e = document.getElementById(id); if (e.style.display == 'block') e.style.display = 'none'; else e.style.display = 'block'; } 
 <a href="#" onclick="toggle_visibility(event,'foo');">School Area</a> <div id="foo" style="display: none;">...</div> 

This is the default behaviour of this element you can use 这是您可以使用的此元素的默认行为

Use javascript:void(0) in href to avoid its behaviour. href使用javascript:void(0)避免其行为。

<a href="javascript:void(0)" onclick="toggle_visibility('foo');">School Area</a>

OR 要么

use preventDefault() in function to prevent its default behavior to execute. 在函数中使用preventDefault()以防止其默认行为执行。

function toggle_visibility(e, id) {
   e.preventDefault();
   var e = document.getElementById(id);
   if(e.style.display == 'block')
      e.style.display = 'none';
   else
      e.style.display = 'block';
}

you may also change from 您也可以从

<a href="#" onclick="toggle_visibility('foo');">School Area</a>

to

<a style="cursor:pointer;text-decoration:underline;color:blue"
  onclick="toggle_visibility('foo');"
>School Area</a>

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

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