jQuery 不推荐使用 getPreventDefault()。请改用 defaultPrevented。为什么我收到这个错误,它的解决方案是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26177305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use of getPreventDefault() is deprecated. Use defaultPrevented instead. Why I'm getting this error and what's the solution for it?
提问by PHPLover
Previously I was using jQuery 1.7.1 in my code. I was getting the above error. Then I used the jQuery 1.11.1 straight from the google repository
以前我在我的代码中使用 jQuery 1.7.1。我收到了上述错误。然后我直接从谷歌存储库中使用了 jQuery 1.11.1
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.1.min.js">
</script>
but still I'm getting this error. How should I resolve this?
但我仍然收到此错误。我应该如何解决这个问题?
Due to this error my other jQuery functionality is also not working.
由于此错误,我的其他 jQuery 功能也无法正常工作。
I researched a lot about the solution but every time I got the same solution of upgrading the jQuery version. But this is also not working for me.
我对该解决方案进行了大量研究,但每次都得到相同的升级 jQuery 版本解决方案。但这对我也不起作用。
回答by cuth
Try:
尝试:
event.originalEvent.defaultPrevented
As in:
如:
$(document).on('click', function (e) {
if (e.originalEvent.defaultPrevented) return;
// continue
});
回答by markl139
I get this error with PHPStorm debugging with Firefox 2.8 when using jQuery, currently jquery-2.0.2.min. On examining the file, it contains the following statement:
使用 jQuery(当前为 jquery-2.0.2.min)时,使用 Firefox 2.8 进行 PHPStorm 调试时出现此错误。在检查文件时,它包含以下语句:
this.isDefaultPrevented=e.defaultPrevented||e.getPreventDefault&&e.getPreventDefault()?U:Y
if you change this to:
如果您将其更改为:
this.isDefaultPrevented=e.defaultPrevented?U:Y
the warning stops.
警告停止。
回答by Martin
I have also come across this problem and found that with jQuery 1.x the replacement
我也遇到过这个问题,发现用 jQuery 1.x 替换
event.defaultPrevented;
does not work at all, but the original
根本不起作用,但原来的
event.getPreventDefault();
still works as expected but does throw a warning on Firebug. I guess someone somewhere expects everyone to upgrade to jQuery 2.x eventually. This shouldn't be a fatal or critical error for you, simply a warning, and in this instance that the replacement feature doesn't work on jQuery 1.x then it's suitable to bare this in mind but not act upon this warning.
仍然按预期工作,但会在 Firebug 上发出警告。我猜有人希望每个人最终都升级到 jQuery 2.x。这对您来说不应该是致命的或严重的错误,只是一个警告,在这种情况下,替换功能在 jQuery 1.x 上不起作用,那么记住这一点是合适的,但不要对这个警告采取行动。
回答by Shea Price
I had the same issue and using Firefox's dev tools I realized I incorrectly commented something out in a hurry, forgetting to comment out the </script>
also. Sometimes it the stupid little things.
我遇到了同样的问题,使用 Firefox 的开发工具时,我意识到我匆忙地错误地注释了一些东西,忘记注释掉了</script>
。有时它是愚蠢的小事。
回答by pollux1er
I suggest you to use the file locally.
Then if the problem is still there, open your jquery file and search for "getPreventDefault
" and replace by "defaultPrevented
".
我建议您在本地使用该文件。然后如果问题仍然存在,请打开您的jquery文件并搜索“ getPreventDefault
”并替换为“ defaultPrevented
”。
回答by Farid Abbas
kindly check if the jquery.min.js is included twice. if yes then try to ignore local one so that you get latest file. Actually I got same msg on myside so by debugging i found there were to jquery library was included twice. hope this will work for you.
请检查 jquery.min.js 是否包含两次。如果是,则尝试忽略本地文件,以便获得最新文件。实际上,我在我身边得到了相同的 msg,所以通过调试我发现 jquery 库被包含了两次。希望这对你有用。
回答by anatoly techtonik
There is getPreventDefault
in https://code.jquery.com/jquery-1.7.1.min.js
有getPreventDefault
在https://code.jquery.com/jquery-1.7.1.min.js
The list of all versions https://code.jquery.com/jquery/
所有版本列表https://code.jquery.com/jquery/
getPreventDefault
is still in 1.8.3, 1.9.1, 1.10.2, 1.11.0
getPreventDefault
仍然在1.8.3, 1.9.1, 1.10.2, 1.11.0
And it's finally gone in 1.11.1of which 1.11.3is the latest version.