Friday 13 July 2012

jQuery selectors code snippets demo

Selector is the most basic things in jQuery. Their use is very simple and straight and correct use ofselectors can enhance the efficiency of writing jQuery code. I have put together some of the common selectors which are pretty common.

ID Selector
1$(document).ready(function () {
2   $('#dvDummy').css('background''#000000');
3});
Class Selector
1$(document).ready(function () {
2   $('.class1').css('background''#000000');
3});
Element Selector
1$(document).ready(function () {
2   $('p').css('font-size''14px');
3});
Selector (loop through all elements)
1$(document).ready(function () {
2   $('form *').css('color''#FFFF00');
3});
Select Multiple Elements
1$(document).ready(function () {
2   $('p, div').css('margin''0');
3});
The parent > child (immediate child element)
1$(document).ready(function () {
2   $('div > span').css('color''#FF0000');
3});
:first and :last (take the first element or the last element)
1$(document).ready(function () {
2   $('span:first').css('color''#FFFF00');
3   $('span:last').css('color''#FFFF00');
4});
:even and :odd (Select elements with an even index or odd index elements. The index starts from 0.)
1$(document).ready(function () {
2   $('tr:even').css('color''#00FF00');
3   $('tr:odd').css('color''#0000FF');
4});
:eq(x) (Selects element with the specified index)
1$(document).ready(function () {
2  $('tr:eq(2)').css('background''#FFFF00');
3});
:contains(text) (Select element which contains specified text)
1$(document).ready(function () {
2  $('div:contains("jQuery")').css('color''#FFFF00');
3});
:hidden (Selects hidden elements)
1$(document).ready(function() {
2  $('div:hidden').show(500);
3});
:visible (Selects visible elements)
1$(document).ready(function() {
2  $('div:visible').css('color''#FFFF00');
3});

No comments:

Post a Comment

What should you required to learn machine learning

  To learn machine learning, you will need to acquire a combination of technical skills and domain knowledge. Here are some of the things yo...