使用用户脚本将 jQuery 和 jQuery UI 插入标头
我在尝试通过我正在处理的 Greasemonkey 用户脚本将 jQuery 和 jQuery UI 插入网页标题时遇到了麻烦。下面是我插入它的方式:
var ccst1 = document.createElement("script");
ccst1.id = "ccst1";
ccst1.src = "http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js";
ccst1.type = "text/javascript";
document.head.appendChild(ccst1);
var ccst2 = document.createElement("script");
ccst2.id = "ccst2";
ccst2.src = "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js";
ccst2.type = "text/javascript";
document.head.appendChild(ccst2);
实际的插入过程是有效的,但是当我插入它时,在页面上使用 jQuery(例如在 html onclick 中)可以工作,但是 jQuery UI 立即给我一个“$ 未定义”错误并且在插入时出现“jQuery 未定义”错误。然后 jQuery UI 无法在页面上运行。
I've been having trouble trying to insert jQuery and jQuery UI into webpage headers through a Greasemonkey userscript I'm working on. Here's how I'm inserting it:
var ccst1 = document.createElement("script");
ccst1.id = "ccst1";
ccst1.src = "http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js";
ccst1.type = "text/javascript";
document.head.appendChild(ccst1);
var ccst2 = document.createElement("script");
ccst2.id = "ccst2";
ccst2.src = "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js";
ccst2.type = "text/javascript";
document.head.appendChild(ccst2);
The actual insertion process works, but when I've inserted it, using jQuery on the page (e.g. in an html onclick) works, but jQuery UI immediately gives me a "$ is not defined" error AND a "jQuery is not defined" error the second it's inserted. No jQuery UI then works on the page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信您所需要的只是在插入需要 jQuery 的新代码之前稍等一下。
I believe what you need is simply a small wait before inserting the new code that requires jQuery.
jquery 是否已在您尝试将其包含到的页面中定义。
你有没有尝试过
它是否在裸函数内
Is jquery already defined in the page you are trying to include it into.
Have you tried
Is it inside a naked function
我有两种方法将 jQ 与我的用户脚本一起使用。第一个是将 jQ 添加到用户脚本中。
这可行,但事件可能会变得棘手。如果页面已经使用 jQ (检查源代码),我喜欢做的实际上是创建 2 个文件。一个 user.js(GM 脚本)将 a 注入到头部。第二个文件需要托管在某个地方(我使用保管箱),但这是所有培根所在的位置。您可以像编写页面代码而不是 GM 脚本一样编写 jQ。
例如
GM.user.js
example.js
无论您选择哪种方法,祝您好运并玩得开心。
I have two methods to using jQ with my userscripts. The first is to add jQ to the userscript.
This works, however events can get tricky. What I like to do, if the page already is using jQ (check source) is actually create 2 files. One user.js (the GM script) that injects a into the head. This second file needs to be hosted somewhere (i use dropbox) but this is where all your bacon is located. You can write your jQ as if you are writing the code for the page and not a GM script.
eg
GM.user.js
example.js
Whichever method you pick, good luck and have fun.
我刚刚找到了解决这个问题的方法。它实际上确实与 jQuery 未正确加载有关,但其他解决方案都不适合我。 Joey Geralnik 建议这样做,效果正常:
I've just found a solution to this. It actually did have to do with jQuery not being loaded properly, but none of the other solutions worked for me. Joey Geralnik suggested this, which is working properly: