简体   繁体   English

这两个var $ test.parent()和var test.parent之间在用法上有什么区别吗?

[英]Is there any usage difference between these two var $test.parent() and var test.parent?

I have two variable and the bellow code to access the elements. 我有两个变量和下面的代码来访问元素。 I have access to both variables parent element as well as its child elements. 我可以访问父元素及其子元素这两个变量。 I can not see any difference. 我看不出任何区别。 Are they same or is there an any difference? 他们是一样的还是有什么不同?

HTML HTML

<div>Sample text : 
    <input id='test' type='text' value='test' />
</div>

JavaScript JavaScript的

$(function(){
        var test = $('#test');
        var $test = $('#test');
        console.log($test.parent().text());
        console.log(test.parent().text());
});

I can see 3 possible aspects to this question, so I will answer all three: 我可以看到此问题的三个可能方面,因此我将回答所有三个方面:

The $ sign $符号

In JavaScript the $ is just another character which can be used as part of a variable name, and is part of the name. 在JavaScript中, $只是另一个字符,可以用作变量名的一部分,并且是名称的一部分。 This is unlike PHP, where the $ sign is use as a prefix, and is not part of the name. 这与PHP不同,PHP中的$符号用作前缀,而不是名称的一部分。

Just like any other valid character (except numerals) the $ can be used by itself as a valid name, and jQuery has used this to name their main function (AKA jQuery ). 就像其他任何有效字符(数字除外)一样, $本身也可以用作有效名称,而jQuery已使用$来命名其主要功能(AKA jQuery )。

You can choose to name your variables anything you like, but many developers like to start some with $ sign as a reminder that it represents a jQuery object which is non-trivial object. 您可以选择使用任何您喜欢的变量命名,但是许多开发人员喜欢以$符号开头,以提醒它表示一个jQuery对象,这是不平凡的对象。

In other words, it makes no difference whether you decide to begin with the $ or not, as long as you remember what you've done. 换句话说,只要您记得自己所做的一切,决定是否以$开头都没有区别。

Are the 2 variables the same? 这两个变量是否相同?

Apropos the above, the two variable names are not the same. 按照以上所述,两个变量名称相同。 One has 4 characters, and one has 5. They don't have any relationship to each other at all. 一个拥有4个字符,一个拥有5个字符。它们根本没有任何关系。

Are the two objects the same? 这两个对象是否相同?

Every time you run the jQuery function, you get a new object. 每次运行jQuery函数时,都会得到一个对象。 This object contains, among other things a reference to the DOM object referred to in your argument. 除其他事项外,该对象还包含对您的参数中引用的DOM对象的引用。

Even though the two jQuery objects refer to the same DOM object, the objects themselves are not the same. 即使两个jQuery对象引用相同的DOM对象,这些对象本身也不相同。

This is a trap that many new jQuery developers fall in: 这是许多新的jQuery开发人员都陷入的陷阱:

$current=$('div#something');
//  some stuff
if($current==$('div#something')) …; // always false

There you are. 你在这。 Three answers. 三个答案。 Take your pick :) 随便挑:)

 $(function(){ var test = $('#test'); var $test = $('#test'); console.log($test.parent().text()); console.log(test.parent().text()); console.log(test[0] === $test[0]); console.log(test === $test); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>Sample text : <input id='test' type='text' value='test' /> </div> 

No difference-- test is the name of one variable and $test is the name of another-- but they are both set equal to the same thing, and are otherwise handled in the same way. 没什么区别test是一个变量的名称, $test是另一个变量的名称-但它们都被设置为相同的东西,否则将以相同的方式进行处理。 The $ is only significant when used as its jQuery function form $(/*some stuff*/) -- otherwise, it is just a valid character for a variable name. $仅当用作其jQuery函数形式$(/*some stuff*/)时才有意义-否则,它只是变量名的有效字符。

Note, however, that they refer to the same element but the objects themselves are different objects, so not strictly equal . 但是请注意,它们引用相同的元素,但是对象本身是不同的对象,因此不严格相等 Thus why the first comparison above evaluates to "true" but the second evaluates to "false". 因此,为什么上面的第一个比较结果为“ true”,而第二个比较结果为“ false”。 You can also evaluate their equality with the jQuery .is() method. 您还可以使用jQuery .is()方法评估它们的相等性。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM