[英]avoid browser reset from onclick event (prototype)
I have few anchors with a click event: 我的点击事件锚点很少:
$$('.someanchor').invoke('observe', 'click', somefunction );
When the anchor is clicked, the browser scrolls to top. 单击锚点后,浏览器将滚动到顶部。 I tried to insert return false
at the end of somefunction
, but it still scrolls up. 我尝试在somefunction
的末尾插入return false
,但是它仍然向上滚动。
Usually I would use something like: 通常我会使用类似:
Event.observe('somelement', 'click', somefunction, false);
But this doesnt work for a collection of elements. 但这不适用于元素集合。 So how do I return false
from a invoke
statement? 那么,如何从invoke
语句return false
?
在somefunction
,在函数主体的末尾添加以下内容(确保e
是最后一个参数,并且用于Event
对象):
e.preventDefault();
"e.preventDefault()" prevents the anchor default behavior but not the event propagation (bubbling). “ e.preventDefault()”阻止锚默认行为,但不阻止事件传播(冒泡)。 I would use "Event.stop(event)" to neutralize both, just for being on the safe side. 为了安全起见,我将使用“ Event.stop(event)”中和这两者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.