JQuery selector

Any operation of the page needs the support of nodes. How to find the specified node quickly and efficiently is also a key point in front-end development. JQuery provides a series of selectors to help developers achieve this goal, allowing developers to focus less on complex selection processes and performance optimization and more on writing business logic.

JQuery supports almost all the mainstream cSS1 to CSS3 selectors, so we’ll start with the simplest and most common ones

Id selector: an ID used to find the element’s ID attribute

$( “#id” )

Id selectors are also basic selectors, and jQuery internally uses the JavaScript function document.getelementById () to handle id fetching. Native syntax support is always efficient, so it’s worth considering this selector if you can manipulate DOM fetching with ids: ids are unique, and each ID value can only be used once on a page. If multiple elements are assigned the same ID, only the first DOM element in the selection set of that ID will be matched. But that shouldn’t happen; Pages with more than one element using the same ID are not valid

2. The class selector, as the name implies, gets nodes by class style class name

$(“.class”)

Class selectors are less efficient than ID selectors, but the advantage is that they can be selected. The same jQuery implementation, for class selectors, if the browser supports, JQuery is implemented using JavaScript’s native getElementsByClassName() function: $(“.imooc”).css() {$(“.imooc”).css() {$(“.imooc”). There are other advantages that we’ll learn about over time. Element selector: Selects all elements based on the given (HTML) tag name

Description: $(“element”)

Search all nodes with the specified element tag name. This is a collection operation. There is also support for the native getElementsByTagName() function as shown in the example code below:

The < body > < div class = "Aaron" > < p > class = "Aaron" < / p > < p > select < / p > < / div > < div class = "Aaron" > < p > class = "Aaron" < / p > < p > < / p > selected < / div > < div class = "imooc" > < p > class = "imooc" < / p > < p > jQuery selected < / p > < / div > < div class = "imooc" > < p > class = "imooc < / p >" <p> </p> </div> <script type="text/javascript" document.getElementsByTagName('div'); for (var i = 0; i < divs.length; i++) { divs[i].style.border = "3px solid blue"; } </script> <script type="text/javascript"> $("p").css("border", "3px solid red"); </script> </body>Copy the code

The first group: by getElementsByTagName method page, all of the < div > element var divs = document. The getElementsByTagName (‘ div ‘); Divs is a DOM collection object that loops through each

element in the collection to assign two new boder styles: the same effect, $(“p”) takes all

elements and styles them directly using CSS methods

*4, full selector (selector)

In CSS, it is common to write a style like this in the first line

{padding: 0; margin: 0; }

Wildcards mean default margins for all elements. JQuery also allows you to pass in a selector to select elements in a document page

Description: $(“*”)

JQuery aside, if you want to get the document of all the elements in through the document. The getElementsByTagName () passed in the “*” can also be get

It is not difficult to find that id, class, tag can be obtained by the native method of the corresponding node, but we also need to consider a compatibility issue, I mentioned in passing here, for example:

  1. IE implements comment nodes as elements, so a call to getElementsByTagName in IE will include comment nodes, which should not normally be expected
  2. Arguments to getElementById are case insensitive in IE8 and earlier versions
  3. In IE7 and earlier versions, getElementById selects FORM A if the name attribute of form A uses the ID name of another element B and A precedes B
  4. Internet Explorer 8 and earlier, browsers do not support getElementsByClassName

5. Hierarchy selector

All nodes in a document have relationships of one kind or another. We can use the traditional family relationship to describe the relationship between nodes. We can regard the document tree as a family tree. Then nodes and nodes will directly have the relationship of father and son, brother, grandfather and grandson.

The hierarchical selector in the selector is used to handle this relationship: child elements, descendant elements, sibling elements, and neighboring elements

Compare hierarchy selectors with a list

If you look closely, there are many similarities and differences between hierarchical selectors

  1. Each level selector has a reference node
  2. Descendant selectors contain the contents of child selectors’ selections
  3. The normal sibling selector contains the contents of the neighboring sibling selection
  4. Adjacent sibling selectors and regular sibling selectors must select elements under the same parent element

6. Basic filter selector

Most of the time you can’t find the element you want directly through the basic and hierarchical selectors, so jQuery provides a series of filter selectors to make it easier to find the DOM element you want. Many of the filter selectors are not CSS specifications, but are extensions of jQuery’s own selectors for developers’ convenience

Filter selectors are used in much the same way as pseudo-elements in CSS, starting with a colon “:” and going through a list to see how basic filters are described:

Matters needing attention:

  1. :eq(), :lt(), :gt(), :even, :odd are used to filter the set of elements that precede their matching expressions, and further filter based on the elements that were previously matched. Note that jQuery collections are indexed from 0

  2. Gt is a paragraph filter that starts at the next index specified, gt(1) actually starts at 2

7. Content filter selector

Basic filter selectors are for element DOM nodes. If you want to filter by content, jQuery also provides a set of content filter selectors, whose rules are also defined by the child elements or text content they contain

Content filters are described as follows:

Matters needing attention:

  1. Both :contains and :has mean lookup, but contains looks for elements that contain “specified text”, and HAS looks for elements that contain “specified elements”
  2. If :contains matches text contained in a child element of the element, it is also considered eligible.
  3. :parent and :empty are opposites in that they involve child elements, including text nodes

Visibility filter selector

Elements have visible and hidden states, and jQuery extends the visibility filter selectors: Visible and :hidden based on the state of the element

The description is as follows:

Both selectors are extensions of jQuery and look relatively simple, but element visibility depends on the style being applied

The :hidden selector contains not only elements with style display=” None “, but also hidden forms, visibility, and so on

There are several ways we can hide an element:

  1. The CSS display value is None.
  2. Form element with type=”hidden”.
  3. The width and height are explicitly set to 0.
  4. An ancestor element is hidden and is not displayed on the page
  5. The value of CSS visibility is hidden
  6. CSS opacity is 0

Elements are considered visible if they occupy a certain amount of space in the document. The width or height of the visible element is greater than zero. The elements’ visibility: hidden or opacity: 0 are considered visible because they still occupy the spatial layout.

9. Attribute filter selector

Attribute selectors allow you to locate an element based on attributes. You can specify only an attribute of the element so that all elements that use the attribute regardless of its value are located, or you can be more explicit and locate elements that use a specific value on those attributes, which is where attribute selectors show their power.

Browser support:

  • [att = val], [att], [att | = val], [att ~ = val] belongs to the CSS 2.1 specification
  • [ns | attr], [att ^ = val], [att * = val], [att $= val] belong to the range of specification
  • [name!=”value”] belongs to the selector of the jQuery extension

CSS selectors are supported by IE7 and IE8, both CSS2.1 and CSS3, webKit, Gecko, and Opera. Only IE6 and IE6 do not support CSS selectors

Of all the property selectors [attr=”value”] and [attr*=”value”] are the most practical

[attr=”value”] helps us locate different types of elements, especially operations on form elements, For example,input[type=” text”],input[type=”checkbox”], etc. [attr*=”value”] can help us match different types of files in a website

Child element filter selector

Child filter selectors are not used very often and their filtering rules are slightly more complex than the others

Child element filter selector description table:

Matters needing attention:

  1. :first matches only one single element, but the :first-child selector can match more than one: that is, the first child for each parent element. This equals :nth-child(1)
  2. :last matches only a single element, and the: last-Child selector can match multiple elements: that is, matches the last child element for each parent element
  3. :first-child and :last-child are the same if there is only one child
  4. :only-child Matches if an element is the only child of the parent element
  5. Nth-child (n) is strictly from the CSS specification, so the n value is “index”, that is, counting from 1, : nth-Child (index) starts from 1, and eq(index) starts from 0
  6. Nth-child (n); nth-last-child(n); nth-last-child(n); nth-last-child(n

Form element selector

Whether submitting or passing data, form elements play an important role in dynamic interactive pages. JQuery includes a form selector, which makes it extremely easy to get form elements of a particular type

Form selector method description:

I am currently on the job Java development, if you now also want to learn Java development technology, in the process of learning Java to meet any learning method, learning route, learning efficiency and other aspects of the problem, you can apply to join my Java learning exchange skirt: front: 893 middle: 241 back: 279. Inside gathered some are self-learning Java beginners, career changers, beginners, I also have here I do Java technology this period of sorting out some Java learning manual, Java factory interview questions, Java syntax PDF, if you need to find skirt pig access.

Matters needing attention:

With the exception of the Input filter selector, almost every form category filter corresponds to a Type value for an input element. Most form category filters can be replaced with property filters. For example: (‘ password ‘) = = (‘ : password ‘) = = (‘ : password ‘) = = (‘ [type = “password,”]).