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.pswp.cn/news/540680.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540680.shtml
英文地址,請注明出處:http://en.pswp.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中使用了不同的數字運算管理員來表示特定的…