简体   繁体   English

我如何使用jQuery.noConflict();

[英]How do I use jQuery.noConflict();

After installing lightbox for my website my comments stopped working. 在为我的网站安装灯箱后,我的评论停止了工作。 I looked at the code and moved the javascript links that were in the <head> (the ones that came with lightbox) in the to above the javascript links that came with Wordpress-Buddypress. 我看了一下代码,并将<head> (灯箱附带的)中的javascript链接移到了Wordpress-Buddypress附带的javascript链接上方。 Before my javascript links from Lightbox were below the javascript links that came with Wordpress. 在我来自Lightbox的JavaScript链接之前,是Wordpress随附的JavaScript链接下方。

After making the switch, the comments started working again on my website but now the lightbox does not work. 进行切换后,评论在我的网站上再次开始起作用,但现在灯箱不起作用。

When I use Firebug to find errors, I get this error. 当我使用Firebug查找错误时,出现此错误。

$("#videogallery a[rel]").overlay is not a function

That code comes from "videolightbox.js" which was a file that came with my lightbox. 该代码来自“ videolightbox.js”,这是我的灯箱随附的文件。

I did lots of reseach on this problem and I am thinking that I might need to use jQuery.noConflict(); 我在这个问题上做了很多研究,我认为我可能需要使用jQuery.noConflict(); but I have no idea how to use it? 但我不知道如何使用? I was looking at this link 我在看这个链接

but I can't seem to get it to work because I have no idea how to use it. 但我似乎无法使其正常工作,因为我不知道如何使用它。 I also tried replacing all the $() with jQuery() but that did not solve my problem. 我也尝试用jQuery()替换所有的$() ,但这并不能解决我的问题。

but I have no idea how to use it? 但我不知道如何使用?

var $j = jQuery.noConflict();

and your code will change from $(selector) to $j(selector) 并且您的代码将从$(选择器)更改为$ j(选择器)

A lot of examples are given here http://api.jquery.com/jQuery.noConflict/ 这里提供了许多示例http://api.jquery.com/jQuery.noConflict/

One thing to consider closely is the script loading order and watch for repeat loading of JQuery. 需要仔细考虑的一件事是脚本加载顺序,并注意重复加载JQuery。 In some (hopefully rare) situations, you can load JQuery, set up some code that uses it, then reload JQuery which tosses away the events that were set up after loading it the first time. 在某些(希望很少)的情况下,您可以加载JQuery,设置一些使用它的代码,然后重新加载JQuery,从而丢弃第一次加载后设置的事件。

I know I ran into that situation once, but I can't remember the details that led to it occurring. 我知道我曾经遇到过这种情况,但是我不记得导致这种情况发生的细节。

To your answer 对你的回答

$=jQuery.noConflict();

Now you can use $('#id').css(...); 现在您可以使用$('#id')。css(...);

The error is probably causing the rest of the code not being executed, .overlay is not a function means that .overlay is not a function and $ is working fine. 该错误可能导致其余代码未执行, .overlay不是函数意味着.overlay不是函数并且$工作正常。

var test; test("#videogallery a[rel]").test()
//TypeError: test is not a function <-- You would see this if $ wasn't working

var test = function(){return {};};
test("#videogallery a[rel]").test();
//TypeError: test("#videogallery a[rel]").test is not a function <-- You see this because .overlay isn't working

It simply means you are trying to call .overlay before it exists. 这只是意味着您正在尝试调用.overlay之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM