jQuery中的属性选择器中的美元符号是什么意思? [英] What does the dollar sign mean inside an attribute selector in jQuery?

查看:180
本文介绍了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')

所以我的问题是:

  1. 在此示例中$$=的作用
  2. 这段代码有什么作用?
  1. What does the $ or $= do in this example
  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆