javascript 我们是“声明”jQuery 还是简单地使用它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8586195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:54:18  来源:igfitidea点击:

Do we 'declare' jQuery or we simply use it?

javascriptjquery

提问by Rohan

I was asked in an interview that how do you 'declare'jQuery? He did not mean a jQuery variable or a $(func()). In case you find this question weird, please do not penalize me for this as I'm enquiring just cause I was asked. :)

我在一次采访中被问到你如何“声明”jQuery?他的意思不是 jQuery 变量或$(func()). 如果您觉得这个问题很奇怪,请不要因此而惩罚我,因为我只是因为被问到而询问。:)

回答by isNaN1247

You don't "declare" jQuery, you just include the file within a script tag:

你不“声明”jQuery,你只是在脚本标签中包含文件:

<script src="/locationof/jQuery.js"></script>

If you look in the jQuery sourceit appends itself to window.$and window.jQuerywhen it runs as the source code is in a self-executing anonymous function.

如果您查看jQuery 源代码,它会将自身附加到,window.$并且window.jQuery当它运行时,源代码在一个自动执行的匿名函数中。

(function( window, undefined ) {
   // rest of source here

   // Expose jQuery to the global object
   window.jQuery = window.$ = jQuery;
})(window);

回答by Kath

We don't "declare" jQuery, we just add "reference" to the file where is this library in. For example:

我们不“声明”jQuery,我们只是在这个库所在的文件中添加“引用”。例如:

<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>

That's it.

而已。

回答by defau1t

I guess he was assuming that you say using a $sign

我猜他是假设你说使用一个$标志

回答by matino

Perhaps he meant how to include it:

也许他的意思是如何包含它:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>