jquery/javascript中$(window)和window有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16939233/
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
What is the difference between $(window) and window in jquery/javascript
提问by Nikola
What is the difference between javascript window and jquery $(window)?
javascript window 和 jquery $(window) 有什么区别?
I tried in the Chrome console and I get this:
我在 Chrome 控制台中尝试过,我得到了这个:
So, I would conclude is "just" a window object wrapped in a jquery object in a way that then I can use jquery's functions on it (like height(), width(), etc...)
所以,我的结论是“只是”一个包含在 jquery 对象中的窗口对象,然后我可以在其上使用 jquery 的函数(例如 height()、width() 等...)
I did try googling, and stackoverlowing :) OFC, but to no luck.
我确实尝试过谷歌搜索和 stackoverlowing :) OFC,但没有运气。
采纳答案by Elias Van Ootegem
When you write $(window)
, you should know that thatpiece of code is going to run on the JS engine. Have you ever wondered why jQuery objects all have parentheses around them? It is because $
is a function object. Basically you're calling the $
function, and passing the nativeglobal, or window
object to it as an argument.
当你写$(window)
,你应该知道,这一段代码是要在JS引擎上运行。你有没有想过为什么 jQuery 对象周围都有括号?这是因为它是$
一个函数对象。基本上,您正在调用该$
函数,并将本机全局或window
对象作为参数传递给它。
If you browse through the jQuery source code, you'll see that it'll pass that object on to many internal functions and in the end, it'll return a jQuery wrapper object.
So yes, your assumptions are pretty much correct.
如果您浏览 jQuery 源代码,您会看到它会将该对象传递给许多内部函数,最后,它会返回一个 jQuery 包装器对象。
所以是的,你的假设非常正确。
回答by Suresh Atta
You are ture
你是真的
window, which is a jQuery wrapper containing the global window object. The intent here was to create a locally-scoped window variable that would give me immediate access to jQuery methods like width(), height(), scrollLeft(), and scrollTop().
window,它是一个包含全局 window 对象的 jQuery 包装器。这里的目的是创建一个局部范围的窗口变量,让我可以立即访问 jQuery 方法,如 width()、height()、scrollLeft() 和 scrollTop()。
回答by user2424941
The window
object represents the window itself. You can find more explanation here. From what you describe above, it seems that you are looking to access the document
properties instead of window
properties. You can access the properties length, height, etc. as follows:
该window
对象代表窗口本身。您可以在此处找到更多说明。从您上面的描述来看,您似乎正在寻找访问document
属性而不是window
属性。您可以按如下方式访问属性长度、高度等:
- document.height (pure javascript) or $(document).height() (jQuery)
- document.width (pure javascript) or $(document).width() (jQuery)
- document.height (纯 javascript) 或 $(document).height() (jQuery)
- document.width(纯 javascript)或 $(document).width() (jQuery)
For more document
properties, see here.
有关更多document
属性,请参见此处。
回答by K D
window is a global object and have no relation to any 3rd party library. however $(window) returns a jQuery object. You are right that its nothing but a wrapper but it comes with all possible jQuery goodies. We can use it just like normal jQuery object we can access its childs, can associate data wtih it etc etc.
window 是一个全局对象,与任何 3rd 方库都没有关系。然而 $(window) 返回一个 jQuery 对象。你是对的,它只是一个包装器,但它带有所有可能的 jQuery 好东西。我们可以像普通的 jQuery 对象一样使用它,我们可以访问它的子对象,可以将数据与它关联等等。