简体   繁体   English

jQuery / JavaScript,为什么需要将变量包装在$()中才能使用它们?

[英]jQuery / JavaScript, why do I need to wrap variables in $() to use them?

Say I have a map on an array of elements. 假设我有一系列元素的地图。 The callback function takes the index and the value at that position in the array. 回调函数采用索引和数组中该位置的值。

If I wrap the array element that the callback receives in $(), it behaves as I expect. 如果我将回调接收的数组元素包装在$()中,它的行为将与我预期的一样。 If I use it without wrapping it in $(), it gives an error. 如果我使用它而不将其包装在$()中,则会出现错误。

var nonHiddenElements = $( "form :input" ).not(':hidden');

nonHiddenElements.map(function(index, element){

    input_id = $(element).attr('id');  // this works

    input_id = element.attr('id') ;    // this gives an error

})

Can someone explain how this works. 有人可以解释这是如何工作的。 Is this a jQuery quirk, or a JavScript thing? 这是jQuery的怪癖还是JavScript?

What type of objects does my nonHiddenElements array contain exactly? 我的nonHiddenElements数组到底包含什么类型的对象? What is element that gets passed to the callback? 传递给回调的元素是什么? And mainly what is the $() doing? 主要是$()在做什么?

You need to understand how jQuery actually works. 您需要了解jQuery的实际工作方式。 I will try to explain it briefly. 我将尝试简要地解释一下。

$ is nothing but a normal javascript function. $只是普通的javascript函数。 jQuery === $ , is just a function with a fancy name. jQuery === $ ,只是一个名字很漂亮的函数。 This function does a lot of different things, depending on what you pass in it. 此函数执行许多不同的操作,具体取决于您传递的内容。 For example if you pass a string it will be treated as CSS selector and jQuery internals will try to find corresponding DOM elements. 例如,如果您传递一个字符串,它将被视为CSS选择器,而jQuery内部将尝试查找相应的DOM元素。 Or if you pass a string starting with < and ending with > jQuery will create a new DOM element by provided HTML string. 或者,如果你传递一个字符串开始, <和结尾> jQuery将创建提供HTML字符串的新的DOM元素。

Now if you pass a DOM element or NodeCollection of DOM elements, it/they will be wrapped into jQuery instances so that they can have a jQuery prototype methods. 现在,如果您传递一个DOM元素或DOM元素的NodeCollection,它将被包装到jQuery实例中,以便它们可以具有jQuery原型方法。 There are many prototype methods jQuery offers. jQuery提供了许多原型方法。 For example text , css , append , attr - those are all methods of jQuery prototype. 例如textcssappendattr这些都是jQuery原型的方法。 They are defined basically like this (simplified): 它们的定义基本上是这样的(简化的):

jQuery.prototype.text = function() { ... }

Normal DOM elements don't have those convenient methods jQuery provides. 普通的DOM元素没有jQuery提供的便捷方法。 And inside of methods like map or each if you check this value or element parameter like you do, you will see that they are actually not jQuery instances: map或方法的each方法内部,如果像您一样检查this值或element参数,您将看到它们实际上不是jQuery实例:

element instanceof jQuery // => false

and of course you can't use instance methods with not an instance. 当然,您不能在没有实例的情况下使用实例方法。

So in order to use jQuery prototype methods you need have a jQuery instance, which you can obtain if you call jQuery function with DOM element passed in it: 因此,为了使用jQuery原型方法,您需要有一个jQuery实例,如果您在传入DOM元素的情况下调用jQuery函数,则可以获取该实例:

$(element) instanceof jQuery // true

Javascript is a programming language. Javascript是一种编程语言。

jQuery is a JavaScript Library. jQuery是一个JavaScript库。

With jQuery: 使用jQuery:

$("some element")

In native JavaScript you would have to do something like this. 在本地JavaScript中,您将必须执行以下操作。

getElementById('elementByID')

Explained in detail here: https://developer.mozilla.org/en-US/docs/Web/API/document.getElementById 此处详细说明: https : //developer.mozilla.org/en-US/docs/Web/API/document.getElementById

MDN is a great resource for beginners. 对于初学者来说,MDN是一个很好的资源。 https://developer.mozilla.org/en-US/docs/Web/JavaScript https://developer.mozilla.org/zh-CN/docs/Web/JavaScript

暂无
暂无

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

相关问题 (javascript)为什么我需要为事件处理程序使用wrap函数? - (javascript) why do i need to use a wrap function for event handlers? 为什么我需要将JQuery代码包装在函数文字中? - Why do I need to wrap my JQuery code in a function literal? 为什么在javascript中需要变量之间的+? - Why do you need the + between variables in javascript? 我需要了解良好的javascript才能使用jquery吗? - Do I need to know good javascript in order to use jquery? 如何从php的会话数组中获取未知数量的元素,并将其用作JavaScript中的单独变量? - How do I take an unknown number of elements from a session array in php and use them as separate variables in JavaScript? 有很多变量是有序的。 我需要使用 JavaScript 将它们转换为包含其他内容但不想一一进行 - Have a lot of variables that are in order. I need to convert them using JavaScript to contain something else but don't want to do it one by one 尝试多次调用它们或将它们分配给局部变量时,为什么Javascript中的类变量消失了? - Why do class variables in Javascript disappear when trying to call them multiple times or assigning them to local variables? 我是否需要使用JQuery删除和替换事件处理程序以交换它们? - Do I need to remove and replace event handlers with JQuery to swap them? JavaScript图像翻转-需要字幕,如何添加字幕? - Javascript image rollover - need captions, how do I add them? jQuery-为什么在这种情况下需要live()? - jquery - why do i need live() in this situation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM