jQuery selector

What is a jQuery selector?

jQuery selector is one of the very important parts of the jQuery library. It supports CSS syntax familiar to web developers to set up pages quickly and easily. Understanding jQuery selectors is the key to opening the door to efficient jQuery development. A typical jQuery selector syntax form:
$(selector).methodName();
selector is a string expression that is used to identify elements in the DOM and is then provided using jQuery set of methods.
In most cases jQuery supports such operations:
$(selector).method1().method2().method3();
This example represents the id in the implicit DOM ="goAway" element, and then add a class="incognito" attribute to it.
$('#goAway').hide().addClass('incognito');
Tips: When the selector expression matches multiple elements, it can be used conveniently and flexibly like JavaScript array operations. Array pointer for selection. This is an example:
var element = $('img')[0];
Among the elements represented by the matching expression, the first element object will be assigned to the variable element.

Basic selector

1. Element selector

jQuery element selector is based on elements The name of the selected element.

    php中文网(php.cn)   

标题

段落。

另一个段落。

2. #id selector

    php中文网(php.cn)   

段落

另一个段落

3.class selector

    php中文网(php.cn)   

标题

段落。

4.element selector

Set the text size of the p element to 12px:

$(document).ready(function () {
$('p').css('font-size' , '12px');
});

5. * Selector

$(document ).ready(function () {
). ## });

6. Parallel selector

$(document).ready(function () {// Set the margin of the p element and div element to 0$('p, div').css('margin', '0');
});


Other hierarchical selectors and filter selectors will be introduced step by step in subsequent chapters.

Using jQuery functions in separate files

If your website contains many pages and you want your jQuery functions to be easy to maintain, then please put your jQuery functions into separate files .js file.

When we demonstrate jQuery in the tutorial, we add the function directly into the section. However, it would be better to put them in a separate file, like this (referencing the file via the src attribute):

   


Continuing Learning
||
php中文网(php.cn)

选择器是jQuery最基础的东西

选择器是jQuery最基础的东西

submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!