javascript 如何在不破坏其他东西的情况下修复“element.dispatchEvent is not a function”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27080518/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:55:22  来源:igfitidea点击:

How to fix “element.dispatchEvent is not a function” without breaking something else?

javascriptjqueryeventsdispatchevent

提问by gstackoverflow

I have same problem as "element.dispatchEvent is not a function" js error caught in firebug of FF3.0but in google chrome.

我有与“element.dispatchEvent 不是函数”相同的问题,在 FF3.0 的萤火虫中但在谷歌浏览器中捕获了 js 错误

I have wrote <script>jQuery.noConflict();</script>after all scripts. But now I have another problem:

我写<script>jQuery.noConflict();</script>了所有的脚本。但现在我有另一个问题:

  ...
  $("a.try").click(function () {
  ...

undefined is not a function

undefined 不是函数

and

 ...
 var blockHeight = $(window).height();
 ...

undefined is not a function

undefined 不是函数

Thus first fix is not valid.

因此,第一次修复无效。

P.S.

聚苯乙烯

html part where I include scripts:

我在其中包含脚本的 html 部分:

....
<script type="text/javascript"
        src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script type="text/javascript"
        src="<c:url value='/resources/js/underscore.js'/>"></script>
<script type="text/javascript"
        src="<c:url value='/resources/js/jquery-1.8.2.min.js'/>"></script>

<script type="text/javascript"
        src="<c:url value='/resources/js/jquery.mousewheel.js'/>"></script>
<script type="text/javascript"
        src="<c:url value='/resources/js/popup.js'/>"></script>
<script type="text/javascript"
        src="<c:url value='/resources/js/jquery.jscrollpane.min.js'/>"></script>

<script type="text/javascript"
        src="<c:url value='/resources/js/scroll-startstop.events.jquery.js'/>"></script>

<script type="text/javascript"
        src="<c:url value='/resources/js/jquery-ui-1.10.0.custom.min.js'/>"></script>

<script type="text/javascript"
        src="<c:url value='/resources/js/script-ini.js'/>"></script>
<script type="text/javascript"
        src="<c:url value='/resources/js/map/map.js'/>"></script>
<script type="text/javascript"
        src="//maps.google.com/maps/api/js?sensor=true&js?v=3.exp&libraries=places"></script>

<script type="text/template" id="terminal-template">
    ...
</script>
<style>
    .grey-terminal {
        background-color: rgb(226, 226, 226);
    }
</style>
<script type="text/javascript"
        src="<c:url value='/resources/js/addTerminal.js'/>"></script>
<script>jQuery.noConflict();</script>
...

Can you advise another way?

你能建议另一种方式吗?

回答by Hyman

When you call jQuery.noConflict();you no longer associate jQuerywith the $variable.

当您调用时,jQuery.noConflict();您不再jQuery$变量关联。

So things like $().clickor $(selector)will throw errors. Instead, you can do something like this:

所以像$().clickor$(selector)会抛出错误。相反,您可以执行以下操作:

jQuery.noConflict();
jQuery('a.try').click(handler);
jQuery(window).height();

Or, you can also assign jQuery to a new variable, like this:

或者,您也可以将 jQuery 分配给一个新变量,如下所示:

var j$ = jQuery.noConflict();
j$('a.try').click(handler);
j$(window).height();

http://jsfiddle.net/wL4mc03a/

http://jsfiddle.net/wL4mc03a/

Edit

编辑

As for dispatching events, one way you can do this (using jQuery) is to triggerthe event. Say you have some code that looks like this:

至于调度事件,您可以执行此操作的一种方法(使用 jQuery)是trigger事件。假设您有一些如下所示的代码:

jQuery(document).on('keydown', function(e){
    if (e.which === 42) {
        alert('That is the key to life!');
    }
});

Later, you can trigger this event. Instead of triggering the event using a string ('keydown'), we'll create an jQuery event and pass that.

稍后,您可以触发此事件。我们将创建一个 jQuery 事件并传递它,而不是使用字符串 ('keydown') 触发事件。

var evt = jQuery.Event('keydown');
evt.which = 42;

jQuery(document).trigger(evt);