jquery选择器连续选择_JQuery中的选择器

jquery选择器连续选择

It's time to write some JQuery now. Do check out the introductory article on JQuery first in case you haven't. Before we move to Selectors in JQuery, let's talk a bit about the general syntax first.

现在该写一些JQuery了。 如果没有,请先查看有关JQuery的介绍性文章 。 在转到JQuery中的Selectors之前,让我们先谈谈常规语法。

陈述 (Statements)

Almost everything in JQuery is a statement. This may not be the first time you're hearing this because most programming languages conceive every distinguishable line of code as a statement. In JQuery, all statements are preceded with a $ (dollar sign). This is also known as the JQuery keyword. For instance,

JQuery中的几乎所有内容都是一个语句 。 这可能不是您第一次听到此消息,因为大多数编程语言将每条可区分的代码行都视为一条语句。 在JQuery中,所有语句都以$(美元符号)开头。 这也称为JQuery关键字 。 例如,

    document.getElementById("#sub-text");
$("#sub-text");

We have used a CSS selector above using JQuery. You can see how easy it is to select an element using a CSS selector in JQuery. You just write the JQuery keyword ($ sign) followed by a pair of parentheses ( () ) and put the CSS selector inside those parentheses.

我们在上面使用JQuery使用了CSS选择器 。 您可以看到在JQuery中使用CSS选择器选择元素有多么容易。 您只需编写JQuery关键字( $ sign ),然后加上一对括号(()) ,然后将CSS选择器放在这些括号内。

The above two statements, the former in Vanilla JS and the later in JQuery essentially do the same thing however there is a small, subtle yet important difference to note. Before we understand that and start coding some JQuery let's create a simple HTML page that we can play around with. I'm taking this boilerplate from the introductory article. All I have done till now is added materialize CDN for writing cool styles quickly and link to our local stylesheet,

上面的两个语句,在Vanilla JS中的前者和在JQuery中的后一个本质上是做相同的事情,但是要注意一个小的,微妙的但重要的区别。 在我们了解这一点并开始编写一些JQuery代码之前,让我们创建一个可以使用的简单HTML页面。 我将从介绍性文章中摘录该样板。 到目前为止,我所做的只是添加了实现CDN,以便快速编写酷炫的样式并链接到我们的本地样式表,

Index.html

Index.html

<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<!-- Compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="index.js"></script>
</body>
</html>
Let's add some content quickly so we can start using JQuery
<div class="container">
<h1 class="title">Explaining JQuery to Spongebob</h1>
<p id="intro-text">Can I say one word or two about Spongebob first?</p>
<div class="row">
</div>
<div class="card">
<div class="card-content">
<h5 class="sub-title">List of names of spongebob's buddies are</h5>
<ul class="center collection" id="list">
<li>Mr Krabs</li>
<li>Garry</li>
<li>Squidward Tentacles</li>
<li>Mrs Puff</li>
</ul>
</div>
</div>
</div>

Output

输出量

selectors in JQuery | Example 1

Now that we have the template setup, let's first see that subtle difference we spoke about,

现在我们有了模板设置,让我们首先看看我们谈到的细微差别,

    console.log(document.querySelector('.title'));
console.log($('.title'));

The above two statements should essentially do the same thing, and they actually do. They both get us a reference to the HTML element with a class name of the title. However,

上面的两个语句在本质上应该做同样的事情,并且实际上是在做。 它们都为我们提供了带有标题类名HTML元素的引用。 然而,

Output

输出量

    <h1 class="title">Explaining JQuery to Spongebob >/h1>
k.fn.init [h1.title, prevObject: k.fn.init(1)]

The second statement returns us somewhat an array wrapped around the element. It looks a lot like an array however, to be accurate it's a JQuery Object. No matter what you reference to, a JQuery statement always returns a JQuery Object. You can also verify this,

第二条语句返回一些包裹在元素周围的数组。 它看起来很像一个数组,但准确地说,它是一个JQuery对象。 无论引用什么,JQuery语句始终返回JQuery对象。 您也可以验证

    typeof(title);

Output

输出量

    "Object"

Why JQuery wraps elements in a JQuery object wrapper is simply because we have loads of different properties and methods available to us then? This becomes very useful when we're animating elements using JQuery.

为什么JQuery在JQuery对象包装器中包装元素仅仅是因为我们拥有大量可用的不同属性和方法呢? 当我们使用JQuery为元素设置动画时,这将非常有用。

    title.animate;

Output

输出量

    ƒ (t,e,n,r){var i=k.isEmptyObject(t),o=k.speed(e,n,r),a=function(){var e=dt(this,k.extend({},t),o);(i||Q.get(this,"finish"))&&e.stop(!0)};return a.finish=a,i||!1===o.queue?this.each(a):this.queue(o.que...

However, we also have the freedom to unwrap our element and return a regular Vanilla JS object and use the common Vanilla JS methods available on it.

但是,我们也可以自由地展开元素并返回常规的Vanilla JS对象,并使用其上可用的常见Vanilla JS方法。

    title[0];

Output

输出量

    <h1 class="title">Explaining JQuery to Spongebob</h1>

Notice how the 0th element inside the JQuery object is actually that HTML element so we can simply obtain it using the indexing method. However, now we don't have access to the animation methods,

注意,JQuery对象中的第0个元素实际上是那个HTML元素,因此我们可以简单地使用索引方法来获取它。 但是,现在我们无法访问动画方法,

    title[0].animate;

Output

输出量

    ƒ animate() { [native code] }

Selectors are used to grab content from the web page. We can use simple CSS selectors to grab elements from the DOM using JQuery.

选择器用于从网页中获取内容。 我们可以使用简单CSS选择器通过JQuery从DOM中获取元素。

    const title=$('.container h1');
console.log(title);

Output

输出量

    k.fn.init [h1.title, prevObject: k.fn.init(1)]

We can also target ids.

我们还可以定位ID。

    const list=$('#list');
console.log(list);

Output

输出量

	k.fn.init [ul#list.center.collection]
0: ul#list.center.collection
length: 1
__proto__: Object(0)

If you know CSS, using JQuery selectors will come super easy to you.

如果您知道CSS,那么使用JQuery选择器对您来说将非常容易。

    list[0];

Output

输出量

    <ul class="center collection" id="list">...</ul>

We can also get the first <li> using,

我们还可以使用第一个<li>

    const firstFriend=$('ul li:first')[0];
console.log(firstFriend);

Output

输出量

    <li>Mr. Krabs</li>

The *is the universal selectors and grabs the whole HTML of the page.

*是通用选择器,可获取页面的整个HTML。

    const everything=$('*')[0];
console.log(everything);

Output

输出量

    <html lang="en"><head>…</head><body cz-shortcut-listen="true">...</body></html>

翻译自: https://www.includehelp.com/code-snippets/selectors-in-jquery.aspx

jquery选择器连续选择

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540680.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

加拿大大数据:正在升温的大数据市场

产业发展背景 早在2011年5月加拿大广播电视和电信委员会&#xff08;CRTC&#xff09;就发布了新的“国家宽带计划”&#xff0c;该计划显示&#xff0c;到2015年加拿大全体国民将享有5Mbps的宽带接入速度。CRTC表示&#xff1a;“来自市场的资金及有针对性的政府拨款将继续推动…

scala 多线程_Scala中的多线程

scala 多线程Scala多线程 (Scala Multithreading) Multithreading is the concept of using multiple threads simultaneously that allows the program to perform multiple operations simultaneously. 多线程是同时使用多个线程的概念&#xff0c;它允许程序同时执行多个操作…

split注意事项

为什么80%的码农都做不了架构师&#xff1f;>>> 1.特殊字符 “|”&#xff0c;“*”&#xff0c;“^”&#xff0c;"."&#xff0c;“&#xff1a;”&#xff0c;使用此字符作为分割符&#xff0c;必须用\\加以转义 2.同时存在多个特殊字符的时候&#x…

Harbor升级和数据库迁移手册

Harbor升级和数据库迁移手册当升级一个已经存在的Harbor实例到新版本时&#xff0c;需要迁移数据库数据。参考Whats New in Harbor Database Schema查看数据库发生了哪些变化&#xff0c;如果有的话&#xff0c;就需要进行数据库迁移操作&#xff0c;因为迁移可能会改变数据库模…

Floyd Warshall算法

Description: 描述&#xff1a; This is a very popular interview problem to find all pair shortest paths in any graph. This problem has been featured in interview rounds of Samsung. 这是一个非常流行的面试问题&#xff0c;用于在任何图中找到所有对最短路径。 该…

Java多线程系列--“基础篇”09之 interrupt()和线程终止方式

2019独角兽企业重金招聘Python工程师标准>>> Java多线程系列--“基础篇”09之 interrupt()和线程终止方式 概要 本章&#xff0c;会对线程的interrupt()中断和终止方式进行介绍。涉及到的内容包括&#xff1a;1. interrupt()说明2. 终止线程的方式 2.1 终止处于“阻…

mac活动监视器_什么是活动监视器?

mac活动监视器活动监控 (Activity Monitor) Apple OS X provides the services of which one of them is Activity Monitor. Activity Monitor is used to monitor the activities of computer like active processes, processor load, applications that are running, and the…

concurrent包下的Exchanger练习

Exchanger可以在两个线程之间交换数据&#xff0c;只能是2个线程&#xff0c;他不支持更多的线程之间互换数据。 当线程A调用Exchange对象的exchange()方法后&#xff0c;他会陷入阻塞状态&#xff0c;直到线程B也调用了exchange()方法&#xff0c;然后以线程安全的方式交换数据…

Python默认参数

Python | 默认参数 (Python | default parameters) A default parameter is a value provided in a function declaration that is automatically assigned by the compiler if the caller of the function doesnt provide a value for the parameter with the default value. …

最长公共前缀_最长的公共前缀

最长公共前缀Problem statement: 问题陈述&#xff1a; Write a function to find the longest common prefix string amongst an array of strings. 编写函数以在字符串数组中找到最长的公共前缀字符串 。 If there is no common prefix, return an empty string "&quo…

物联网听起来像是一个和互联网不同的网,万物互联又把网给弄丢了,正向我们扑面而来的是万物互联网。...

物联网听起来像是一个和互联网不同的网&#xff0c;"万物互联"又把"网"给弄丢了&#xff0c;正向我们扑面而来的是"万物互联网"。转载于:https://www.cnblogs.com/beingonline/p/7484135.html

sdram trp_TRP的完整形式是什么?

sdram trpTRP&#xff1a;电视收视点 (TRP: Television Rating Point) TRP is an abbreviation of "Television Rating Point". TRP是“电视评分点”的缩写 。 It is a system or standard of measurement which signifies the demand and popularity of a televisi…

Controller计算值传到jsp页面,用session传值

HttpSession session request.getSession(); session.setAttribute("key",value); jap 用 ${key}来接收该值 转载于:https://www.cnblogs.com/douder/p/7484491.html

CBT的完整形式是什么?

CBT&#xff1a;基于计算机的培训 (CBT: Computer Based Training) CBT is an abbreviation of "Computer-based training". CBT是“基于计算机的培训”的缩写 。 It is a training program which entails the use of a personal system or networked computer. The…

论道社会化商业

主持人 用友优普副总裁傅毅&#xff1a; 谢谢各位嘉宾&#xff0c;我们会留一些时间让在座的嘉宾提问。请各位嘉宾用一个非常简单的一句话&#xff0c;或者几个关键词&#xff0c;总结一下你认为的社会化商业是什么&#xff1f; 用友优普执行总裁 徐洋&#xff1a; 社会化商业为…

CChelper彩虹SDK可视远程客服解决方案

本文讲的是 : CChelper彩虹SDK可视远程客服解决方案 , 在智能生态产业链中&#xff0c;智能硬件终端是把握消费者的直接环节&#xff0c;随着物联网时代迈向成熟&#xff0c;智能家居领域的硬件逐渐成为智能硬件终端的主角。目前的市场环境下&#xff0c;智能家居领域的自身硬…

matlab 简介_MATLAB简介

matlab 简介MATLAB简介 (MATLAB Introduction) MATLAB was designed by Cleve Moler for his student in 1970s but after some time jack little, an engineer realized its potential and rewrote it at the MathWorks, and it was rewritten in C language by the date of 1…

Scala中的嵌套循环

Scala中的嵌套循环 (Nested loop in Scala) In programming, a nested loop is used in initializing or iterate multi-dimensional array or to print patterns. Scala provides an efficient method to use nested loops in the programming language. The most used nested…

python基础-字典

字典 # 字典是python基本数据结构之一&#xff0c;相对于列表和元组&#xff0c;他是无序的&#xff0c;每次输出都打乱了顺序&#xff0c;没有下标hello{110:{"name":"alex","age":28,"home":"shandong"},111:{"name&…

sql算术运算符_SQL中的算术运算符

sql算术运算符SQL | 算术运算符 (SQL | Arithmetic Operators) Different number-crunching administrators are utilized in SQL to be specific Addition (), Subtraction (-), Multiplication (*), Division (/), Modulus (%). SQL中使用了不同的数字运算管理员来表示特定的…