This code (taken from this post) has stopped me getting this error message in IE8 when trying to use preventDefault()
:
Error: 'null' is null or not an object
$(document).ready(function () {
$("#submitBtn").on("click", pD(event));
});
function pD(e) {
e = event || window.event;
if (e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
}
It seems to work. However the ment in this post (fourth ment down) has me concerned that maybe this is a bad method. Is it acceptable, or unacceptable in some way? The language of the ment is pretty strong, so it makes me wonder.
This code (taken from this post) has stopped me getting this error message in IE8 when trying to use preventDefault()
:
Error: 'null' is null or not an object
$(document).ready(function () {
$("#submitBtn").on("click", pD(event));
});
function pD(e) {
e = event || window.event;
if (e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
}
It seems to work. However the ment in this post (fourth ment down) has me concerned that maybe this is a bad method. Is it acceptable, or unacceptable in some way? The language of the ment is pretty strong, so it makes me wonder.
Share Improve this question edited Mar 14, 2023 at 18:41 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 28, 2013 at 20:23 12527481252748 15.4k34 gold badges117 silver badges242 bronze badges 4-
4
should it not be
e = e || window.event
– karthikr Commented May 28, 2013 at 20:24 -
1
There's no point having a function argument
e
if the first line of the function immediately overwrites it with another value. Use karthikr's suggestion so that the argument is used as-is if it has a (truthy) value. – nnnnnn Commented May 28, 2013 at 20:24 -
@nnnnnn okay yeah, typo. so now I get the error
Error: Object required
in the lineif(e.preventDefault){
. – 1252748 Commented May 28, 2013 at 20:25 -
1
jQuery's
event
parameter is not a native event object. It's already fixed for you. So why are you trying to fix it again? – user1106925 Commented May 28, 2013 at 20:29
1 Answer
Reset to default 10Firstly the way you are assigning the click handler is incorrect:
$("#submitBtn").on("click", pD(event));
You are invoking the pD()
function and passing its return value to the .on()
method. You need to just pass a reference to the function:
$("#submitBtn").on("click", pD);
Secondly, you are using jQuery, so you don't need to test the e
argument and set it to window.event
because jQuery handles those patibility issues for you. You can use e.preventDefault()
knowing jQuery will just make it happen.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745329705a4622820.html
评论列表(0条)