原型和jQuery和平共存? [英] prototype and jQuery peaceful co-existence?

查看:80
本文介绍了原型和jQuery和平共存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JavaScript知之甚少,但尽管如此,我还是想在我的wordpress博客上拼凑一些东西。它不起作用,我不知道如何解决它,嘿,这就是StackOverflow的用途,对吧?

I know very little about JavaScript but despite this I'm trying to cobble something together on my wordpress blog. It's not working, and I don't know how to resolve it, and hey, that's what StackOverflow is for, right?

首先,错误信息是:

Error: element.dispatchEvent is not a function
Source File: http://.../wp-includes/js/prototype.js?ver=1.6
Line: 3936

它发生在页面加载。我的页面加载处理程序如此注册:

It happens on page load. My page load handler is registered thusly:

Event.observe(window, 'load', show_dates_as_local_time);

如果我禁用其他一些插件,错误消失了,这个(加上谷歌搜索)让我得出结论这是原型和jQuery之间的冲突(一些其他插件使用)。

The error goes away if I disable some other plugins, and this (plus googling) led me to conclude that it was a conflict between prototype and jQuery (which is used by some of the other plugins).

其次我正在遵循wordpress推荐的使用 wp_enqeue_script 将JavaScript中的依赖项添加到原型库如下:

Secondly I'm following the wordpress recommended practice of using wp_enqeue_script to add a dependency from my JavaScript to the Prototype library, as follows:

add_action( 'wp_print_scripts', 'depo_theme_add_javascript' );

function depo_theme_add_javascript() {
    wp_enqueue_script('friendly_dates', 'javascript/friendly_dates.js', array('prototype'));
}

现在我也知道jQuery和Prototype之间存在一些潜在的冲突这是使用jQuery noConflicts 方法解决的。我试过从各个地方打电话,但没有好处。我不认为这是问题,因为a) noConflict 函数仅与 $ 变量,这似乎不是问题,而b)我会期待 wordpress为我排序,因为它可以......

Now I'm also aware that there are some potential conflicts between jQuery and Prototype which are resolved using the jQuery noConflicts method. I've tried calling that from various places but no good. I don't think this is the problem because a) the noConflict function relates solely to the $ variable, which doesn't seem to be the problem here, and b) I would expect wordpress to sort it out for me because it can...

最后,使用Venkman调试器我确定错误消息中引用的元素确实是 HTMLDocument 但也缺少 dispatchEvent 。鉴于它是一种标准的DOM方法,不知道怎么会发生这种情况?

Lastly, using the Venkman debugger I've determined that the element referenced in the error message is indeed an HTMLDocument but also does lack a dispatchEvent. Not sure how this could happen, given it's a standard DOM method?

推荐答案

感谢所有建议。最后我认为肯特的解释是最接近的,基本上相当于原型被打破。 (对不起,如果我错误地总结你:)

Thanks for the suggestions all. In the end I think Kent's explanation was the closest, which basically amounted to "Prototype is broken". (Sorry if I'm summarizing you incorrectly :)

至于 jQuery.noConflict 选项 - 我已经提到了这个问题。当运行此方法时,会有所不同,而我对此几乎没有控制权。正如我所说,我尝试在几个不同的地方(特别是页面标题和我的脚本文件)运行它,没有任何效果。所以,就像我们都喜欢它一样,只需使用 noConflict 而不是这个问题的答案,至少在没有附加信息。

As for the jQuery.noConflict option - I already mentioned this in the question. It makes a difference when you run this method, and I have very little control over that. As I said, I have tried running it in a couple of different places (specifically the page header and also from my script file), to no effect. So, much as we'd all like it to be, "just use noConflict" is not an answer to this question, at least not without additional information.

此外, jQuery.noConflict 似乎是关于 $ 变量,错误点周围的代码根本不处理该变量。当然它们可以间接相关,我没有追踪它。

Besides, jQuery.noConflict seems to be about the $ variable, and the code around the error point does not deal with that variable at all. Of course they could be related indirectly, I haven't tracked it down.

所以基本上我最后用jQuery而不是Prototype重写脚本,它实际上有自己的问题。无论如何,我已经在我的博客上发布了整个战争故事,如果您有兴趣。

So basically I ended up rewriting the script using jQuery instead of Prototype, which actually had its own problems. Anyway I've published the whole war story on my blog, should you be interested.

这篇关于原型和jQuery和平共存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆