在Userscript/GreaseMonKey中,当网站已经使用旧版本时,如何使用较新版本的jQuery?
我正在尝试为已经使用古代jQuery的网站编写一个用户订阅。如果我 @require
在用户标题中的现代版本,我会发现有关如何存在的字段
不存在的控制台错误。由于这两个版本的jQuery不兼容,是否有任何方法可以使用它们而不会冲突?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许有一个更好的解决方案,但这是我想到的:
@require
按照您通常想要的方式,然后在脚本结束时,放置一个 $。noconflict(); 。由于您的某些Userscript功能可能需要使用最新版本,因此将作为最后一行。为了避免考虑这种可能性,您可以像这样编写脚本:这称为 iife 如果您想了解更多。以上有点奇怪。我这样写了它,使下一个示例更容易理解。这产生了相同的效果,但我认为这更惯用。
Maybe there's an even better solution than this, but here's what I've come up with:
@require
jQuery the way you'd normally want, then at the end of your script, put a $.noConflict();. As some of your userscript functions may need to use the latest version, it may not be enough to put $.noConflict(); as the last line. To avoid thinking about that possibility, you can write your script like this:This is called an IIFE if you want to learn more. The above is kind of weird. I wrote it that way to make the next example easier to understand. This achieves the same effect, but in my opinion, it's more idiomatic.