$和jQuery有什么区别

[英]What is the difference between $ and jQuery


When I try to use $("#div_id") in $(document).ready it returns NULL, but when I use jQuery("#div_id") it returns the actual object!

当我尝试在$(document).ready中使用$(“#div_id”)时,它返回NULL,但是当我使用jQuery(“#div_id”)时,它返回实际的对象!

Why is that happening?

为什么会这样?

UPDATE: I tried noConflict method without gaining any hints.

更新:我尝试了noConflict方法而没有获得任何提示。

jQuery.noConflict()
function (a,b){return new c.fn.init(a,b)}

$.noConflict();
TypeError: Object function ()
    {
        return document.getElementById.apply(document, arguments)
    } has no method 'noConflict'

UPDATE 2:

更新2:

$(document).ready(function() {
    debugger;
});

<input type="text" id="test" name="test" value="123" />

When I run the following code in the console i got those results:

当我在控制台中运行以下代码时,我得到了这些结果:

$("#test").val()
TypeError: Cannot call method 'val' of null
jQuery("#test").val()
"123"

Thanks

谢谢

6 个解决方案

#1


17  

See jQuery.noConflict(). Could other javascript libraries on your page be using the $ variable?

请参阅jQuery.noConflict()。您网页上的其他JavaScript库是否可以使用$ variable?

$ is just a variable that is used to alias jQuery and being a variable, anything could be assigned to it.

$只是一个变量,用于别名jQuery并作为变量,任何东西都可以分配给它。

#2


9  

$ and jQuery both are same except $ is just an alias of jQuery which you can change or remove with jQuery.noConflict mode.

$和jQuery都是相同的,除了$只是jQuery的别名,您可以使用jQuery.noConflict模式更改或删除它。

#3


6  

$ is also used in Prototype.js, which is a javascript framework like jQuery. It could be the case that your project also includes references to this framework.

$也用在Prototype.js中,这是一个像jQuery这样的javascript框架。可能是您的项目还包含对此框架的引用。

For more info on how the prototype dollar works, go here.

有关美元原型如何运作的更多信息,请转到此处。

You can make the two work together with the noConflict statement, as stated in the other answers. We're doing this in one of our projects, which uses the $(element) for legacy prototype code, and jQuery(element) for new jQuery code.

您可以将这两个与noConflict语句一起使用,如其他答案中所述。我们在我们的一个项目中执行此操作,该项目使用遗留原型代码的$(元素)和新jQuery代码的jQuery(元素)。

#4


3  

Looks like something has taken the $, to reassign jQuery to $, try going:

看起来有些东西花了$,将jQuery重新分配给$,试着去:

var $ = jQuery;

#5


2  

Maybe you have another library that uses $?

也许你有另一个使用$的库?

#6


1  

jQuery and $ are the same - see jQuery source code:

jQuery和$是一样的 - 请参阅jQuery源代码:

window.jQuery = window.$ = jQuery;

Maybe you use another library that uses variable $ and then you have to solve naming conflict.

也许您使用另一个使用变量$的库,然后您必须解决命名冲突。

<script type="text/javascript">
  jQuery.noConflict();
  jQuery(document).ready(function($) {
    // Code that uses jQuery's $ can follow here.
  });
  // Code that uses other library's $ can follow here.
</script>

Or you can use one of the following snippet (in it's body there will be no conflict of $):

或者您可以使用以下代码段之一(在它的正文中不会有$冲突):

// Method 1
jQuery(document).ready(function($){
    /* some code that uses jQuery $ */ 
});

// Method 2
(function($) {
    /* some code that uses jQuery $ */ 
})(jQuery);

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2011/06/07/bb4871ebb0fb5e61c717294e299c840d.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号