admin管理员组文章数量:1391943
Code below works in IE, but not in FireFox.
I have unsuccessfully tried all solutions proposed in link below.
Function is invoked when ENTER is pressed. Alert fires if placed in first line of function, but first if statement is not processed.
Invoked via: onKeyPress="javascript:isEnter();"
in control markup.
function isEnter(evnt) {
evnt = (evnt) ? evnt : ((event) ? event : null);
if (evnt) {
var charCode = (evnt.charCode || evnt.charCode == 0) ? evnt.charCode : ((evnt.keyCode) ? evnt.keyCode : evnt.which);
if (charCode == 13) {
//do stuff
}
}
}
link text
Code below works in IE, but not in FireFox.
I have unsuccessfully tried all solutions proposed in link below.
Function is invoked when ENTER is pressed. Alert fires if placed in first line of function, but first if statement is not processed.
Invoked via: onKeyPress="javascript:isEnter();"
in control markup.
function isEnter(evnt) {
evnt = (evnt) ? evnt : ((event) ? event : null);
if (evnt) {
var charCode = (evnt.charCode || evnt.charCode == 0) ? evnt.charCode : ((evnt.keyCode) ? evnt.keyCode : evnt.which);
if (charCode == 13) {
//do stuff
}
}
}
link text
Share Improve this question edited Jan 22, 2023 at 16:46 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 30, 2009 at 6:08 pghcpapghcpa 84311 silver badges27 bronze badges4 Answers
Reset to default 2onKeyPress="javascript:isEnter();"
is wrong for two reasons: first, you shouldn't have the javascript:
prefix since the whole contents of a key handler attribute are processed as JavaScript. This won't be preventing your code working since the javascript:
prefix is interpreted as a label. What is preventing it working is the fact that you're not passing an event object to the isEnter
function. Your onkeypress
attribute should be
onkeypress="isEnter(event);"
This should work in most browsers. The reasoning is that the value of an event handler attribute acts as the body of a function which is passed a parameter named event
. In IE event
instead resolves to window.event
, thus making this approach work in most browsers.
You could also simplify your function as follows:
function isEnter(evnt) {
if (evnt)
var charCode = evnt.keyCode || evnt.which;
if (charCode == 13) {
//do stuff
}
}
}
Thanks all.
I got this from another source. It solved the problem and appears pretty close to the answers above, but skips the if(evnt) portion.
As noted above, it is necessary to call with: onkeypress="isEnter(event);"
function isEnter(e) {
e = e || window.event || {};
var charCode = e.charCode || e.keyCode || e.which;
if (charCode == 13) {
// do stuff
}
}
Your code is more confusing that you need it to be:
First Line of function: evnt = evnt || window.event;
That will do the same thing.
Next, the charcode line: charCode = evnt.charCode || event.keyCode || event.which;
This will actually work better than yours because (evnt.charCode || event.charCode == 0)
will always return true.
x == 0
will return true when x
is null
, undefined
, ""
, and 0
.
Now, if I remember my HTML events, I think your javascript:isEnter()
function should look more like this: isEnter(event)
. Your code isn't passing the event to the function on certain browsers (e.g, Firefox).
So, here's what we have:
onKeyPress="isEnter(event)"
function isEnter(e) {
e = e || window.event;
if (e) {
var charCode = e.charCode || evnt.keyCode || evnt.which;
if (charCode == 13) {
//do stuff
}
}
}
I don't remember if that's the best way to get the charCode, but javascript libraries like prototype, jQuery, dojo, will abstract a lot of those browser differences for you if you want to look to one of them. cheers.
You can extend the Event:
jQuery.Event.prototype.isEnter = function() {
return this.which == 13;
}
Then use
e.isEnter();
Note: If you're not using jQuery, just removed the "jQuery." from the code above.
You can make it even better:
$(function(){
$("input:text").keydown(function(e) {
if (e.isEnter()) $(this).trigger("enterPressed", e);
});
});
$(function() {
$("#myfield").bind("enterPressed", function(e) {
console.log("enter pressed");
});
});
Test it: http://jsfiddle/TfE6m/
and here without jQuery: http://jsfiddle/zqFer/
Your extended method and the method which triggers the event can be in a separate file, maybe called events.js, for example. Your code will be more clean!
本文标签: javascriptEnter Key Trap Event in FireFoxStack Overflow
版权声明:本文标题:javascript - Enter Key Trap Event in FireFox - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744717061a2621464.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论