jQuery中的属性选择器中的美元符号是什么意思? [英] What does the dollar sign mean inside an attribute selector in jQuery?
问题描述
有一个jQuery似乎用id="scuba"
查找所有元素,但是随后它使用attr()来提取id? "scuba"是否可以作为ID的一部分,而attr会将整个ID抽出?我从未见过$属性选择器内部,就像下面的示例一样.
There is this jQuery which seems to look for all elements with id="scuba"
, but then it uses attr() to pull the id out? Could "scuba" be part of the id and attr pulls the entire id out? I've never seen the $ inside an attribute selector, just outside like the example below.
$('*[id$=scuba]').attr('id')
所以我的问题是:
- 在此示例中
$
或$=
的作用 - 这段代码有什么作用?
- What does the
$
or$=
do in this example - What does this code do?
推荐答案
美元符号
第一个$
是jQuery()
函数( jQuery对象构造函数)的简写.
The first $
is a shorthand for the jQuery()
function, the jQuery object constructor.
换句话说,这是一个名为$
的变量,已被分配一个名为jQuery
的函数,如在jQuery源的最小版本中所示:window.jQuery = window.$ = jQuery;
In other words, it's a variable called $
that's been assigned a function called jQuery
, as can been seen in the unminified version of the jQuery source: window.jQuery = window.$ = jQuery;
美元等于符号
第二个$
是名为属性以选择器结尾的jQuery选择器的一部分.在属性选择器中使用时,$=
是逻辑运算符,其字面意思是如果左侧值以右侧值结尾,则为 true ".
The second $
is part of a jQuery selector called Attribute Ends With Selector . When used in an attribute selector, $=
is a logical operator that literally means "true if the left-hand value ends with the right-hand value".
此脚本的实际作用
总体而言,此代码段首先选择以scuba
结尾的具有id
属性的任何元素.然后,它从生成的jQuery对象中检索第一个元素的id
值.
Overall, this snippet first selects any element with an id
attribute ending in scuba
. It then retrieves the id
value of the first element from the resulting jQuery object.
这篇关于jQuery中的属性选择器中的美元符号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!