非常奇怪的jQuery错误 [英] Very weird jQuery error

查看:102
本文介绍了非常奇怪的jQuery错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在我的网站上有 正常运行的代码:

So I had code that was working properly on my site:

$("#usr").load("somepage.php",{var1:var1,var2:var2});

但是自从我在导航栏中更改了一些代码以来,jQuery就一直表现得很奇怪.第一个主要问题是该行:

But ever since I changed some code in the navigation bar, jQuery has been acting really strangely. The first major problem was that this line:

var w = $(window).width();

返回错误:object [global] has no method "width()"

这似乎并不重要,因为页面上的所有元素都运行有该错误(好像仍在执行,因为元素仍在放置中)...但是后来我来到了实现的页面代码的第一行,我遇到了以下错误:

And that didn't seem to matter, as all elements on the page functioned with that error (as if it was still being executed, because elements were still being placed)...but then I came to the page that implemented the first line of code, and I ran into the following error:

Cannot call method "load()" of null

果然,我检查了控制台,并且$(#usr")返回null,但是我可以在页面中看到内联ID为usr的HTML行.

Sure enough, I checked the console, and $("#usr") returns null, but I can see the HTML line in the page with the inline id of usr.

这会导致问题,因为我需要从该页面加载数据才能使页面正常运行.但是它变得更加陌生.我以为我只是尝试一个简单的发布请求并获取数据并使用document.getElementById("usr").innerHTML = ...作为替代,但是我从此行中收到以下错误:

This causes a problem because I need to load data from that page for the page to function properly. But it gets even stranger. I thought I would just try a plain post request and take the data and use document.getElementById("usr").innerHTML = ... as a substitute, but I get the following error from this line:

$.post("somepage.php",{var1:var1,var2:var2},function(data){
    document.getElementById("usr").innerHTML = data;
});

错误:

Uncaught TypeError: Object function $(el){if(!el)return null;if(el.htmlElement)return Garbage.collect(el);if([window,document].contains(el))return el;var type=$type(el);if(type=='string'){el=document.getElementById(el);type=(el)?'element':false}if(type!='element')return null;if(el.htmlElement)return Garbage.collect(el);if(['object','embed'].contains(el.tagName.toLowerCase()))return el;$extend(el,Element.prototype);el.htmlElement=function(){};return Garbage.collect(el)} has no method 'post'

jQuery到底发生了什么?

What the heck is going on with jQuery?

我正在从googleapis导入1.8.2

推荐答案

这听起来像很多,就像您正在加载Prototype或MooTools或jQuery之类的东西一样,因此Prototype/MooTools/无论如何正在取代$符号.

That sounds a lot like you're loading Prototype or MooTools or something as well as jQuery, and so Prototype/MooTools/whatever is taking over the $ symbol.

如果发生了这种情况,并且您需要另一个库,则可以使用 jQuery.noConflict() .然后,您可以将符号jQuery而不是$用于jQuery内容,或者将所有jQuery代码放入传递给jQuery.noConflict并接受$作为参数的函数中,如下所示: /p>

If that's what's going on, and you need the other library, you can use jQuery.noConflict(). Then you either use the symbol jQuery instead of $ for your jQuery stuff, or you put all of your jQuery code into a function that you pass into jQuery.noConflict and accept $ as an argument, like so:

// Out here, $ !== jQuery
jQuery.noConflict(function($) {
    // In here, $ === jQuery
});

或者您也可以自己做:

// Out here, $ !== jQuery
jQuery.noConflict();
(function($) {
    // In here, $ === jQuery
})(jQuery);

ready 还将jQuery对象传递给函数,如果您已经在使用.

ready also passes the jQuery object into the function, if you're already using ready.

这篇关于非常奇怪的jQuery错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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