Home > Web Front-end > JS Tutorial > body text

How to use JQUERY's attribute selectors and custom selectors (2)_jquery

WBOY
Release: 2016-05-16 18:08:24
Original
1157 people have browsed it

Example: Bold link text containing "wangorg" characters
css:

Copy code The code is as follows:

.abold{
font-weight:bold;
}

html:
Copy code The code is as follows:

$('document').ready(function(){

$('a[href* =wangorg]').addClass('abold');
})

Attribute selection can also be combined:
$('a[href^=http]
[href*=wangorg]').addClass('abold')

The custom selector is a unique and completely different selector added by JQUERY. The syntax is the same as the pseudo-class selector syntax in CSS. , that is, the selector starts with a colon (:).
For example: Select the second item from the matching div collection with wangorg class, the corresponding syntax is: $('div.wangor:eq(1)')
The CSS selector syntax is $ ('div:nth-child(2)')
Example: Change the background color of the even rows of the table to #ccc
CSS:
Copy Code The code is as follows:

.alt{
backgroud-color:#ccc;
}

HTML:
Copy code The code is as follows:

$('document').ready(function() {

$('tr:odd').addClass('alt')
})

Change all tables in the web page to the above effect:
Copy code The code is as follows:

$('document').ready(function(){
$('tr:nth-child(even)').addClass('alt');
})

Add the font of the table containing the "wangorg" string in the table Thick
Copy code The code is as follows:

$('document').ready(function( ){
$('tr:contains(wangorg)').addClass('abold');
})

Related selector explanation:
:eq(index )
The elements in the result set that are after (greater than) the given index (starting from 0)
:odd
All odd elements in the result set (starting from 0)
:even All even numbers in the result set Element (starting from 0)
:nth-child(even)
The element that is the even-th child element of its parent (counting from 1)
:contains(text) Element that contains the given text text .
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!