【发布时间】:2011-07-18 00:01:52
【问题描述】:
如何将以下内容转换为使用 PrototypeJS Event.observe?
<input type="text" .... onkeypress="if(event.keyCode==13)return false;">
目的是防止在某些情况下不应该播放的叮当声(在IE9中)。
【问题讨论】:
标签: internet-explorer prototypejs dom-events
如何将以下内容转换为使用 PrototypeJS Event.observe?
<input type="text" .... onkeypress="if(event.keyCode==13)return false;">
目的是防止在某些情况下不应该播放的叮当声(在IE9中)。
【问题讨论】:
标签: internet-explorer prototypejs dom-events
使用 Prototype 的事件内容,您引用的内容将如下所示:
var input = /* ...get the `input` element, how will depend on your structure... */;
input.observe('keypress', function(event) {
if (event.keyCode == 13) {
event.stop();
}
});
Event#stop 做了两件事:它阻止默认操作(如果有),并停止事件冒泡。如果您只想阻止默认操作而不是冒泡,请改用Event#preventDefault(原型将确保它存在,即使在不使用它的浏览器上也是如此);如果您只想停止冒泡但不阻止默认操作,请改用Event#stopPropagation(同样,Prototype 会确保它存在)。
就查找input 元素而言:如果它有id,那很容易:
var input = $('theId');
或者,您可以使用$$,通过它的位置或其他属性找到它,它提供了相当强大的选择器(大多数 CSS3)。
【讨论】: