jQuery class selector

Suggested Videos
Part 3 - Benefits of using CDN
Part 4 - jQuery #id selector
Part 5 - jQuery Element Selector

(追記) (追記ここまで)

In this video we will discuss jQuery class selector i.e selecting elements using their class name

(追記) (追記ここまで)

Syntax : $('.class')

jQuery class selectors uses JavaScript's native getElementsByClassName() function if the browser supports it.

$('.small') // Selects all elements with class small
$('.small,.big') // Selects all elements with class small or big
$('div.small,.big') // Selects div elements with class small and any element with class big

Selects all elements with class "small" and sets 5px solid red border
<html>
<head>
<title></title>
<scriptsrc="Scripts/jquery-1.11.2.js"></script>
<scripttype="text/javascript">
$(document).ready(function () {
$('.small').css('border', '5px solid red');
});
</script>
</head>
<body>
<spanclass="small">
Span 1
</span>
<br/><br/>
<divclass="small">
Div 1
</div>
<br/>
<spanclass="big">
Span 2
</span>
<pclass="big">This is a paragraph</p>
</body>
</html>

Selects all elements with class "small" or "big" and sets 5px solid red border
<scripttype="text/javascript">
$(document).ready(function () {
$('.small, .big').css('border', '5px solid red');
});
</script>

Selects all elements with class "small" and all span elements with class "big" and sets 5px solid red border
<scripttype="text/javascript">
$(document).ready(function () {
$('.small, span.big').css('border', '5px solid red');
});
</script>

Selects all elements with class small that are nested in a an element with id=div2 and sets 5px solid red border
<html>
<head>
<title></title>
<scriptsrc="Scripts/jquery-1.11.2.js"></script>
<scripttype="text/javascript">
$(document).ready(function () {
$('#div2 .small').css('border', '5px solid red');
});
</script>
</head>
<body>
<divid="div1"class="small">
DIV 1
</div>
<br/>
<divid="div2">
Div 2
<br/>
<divclass="small">
DIV 3
</div>
<br/>
<spanclass="small">
SPAN
</span>
</div>
</body>
</html>

Selects all elements with class small and sets 5px solid red border. Notice div1 has 2 classes - small and big.
<html>
<head>
<title></title>
<scriptsrc="Scripts/jquery-1.11.2.js"></script>
<scripttype="text/javascript">
$(document).ready(function () {
$('.small').css('border', '5px solid red');
});
</script>
</head>
<body>
<divclass="small big">
DIV 1
</div>
<br/>
<divclass="small">
DIV 2
</div>
</body>
</html>

Selects all elements that has both the classes - small and big. There should be no space between the class names.
<scripttype="text/javascript">
$(document).ready(function () {
$('.small.big').css('border', '5px solid red');
});
</script>

If you have a space between the two class names then we are trying to find descendants, i.e. find elements with class big that are descendants of an element with class small.
<html>
<head>
<title></title>
<scriptsrc="Scripts/jquery-1.11.2.js"></script>
<scripttype="text/javascript">
$(document).ready(function () {
$('.small .big').css('border', '5px solid red');
});
</script>
</head>
<body>
<divclass="small big">
DIV 1
</div>
<br/>
<divclass="small">
DIV 2
<divclass="big">
DIV 3
</div>
</div>
</body>
</html>

Another way to selects all elements that has both the classes - small and big is by using filter method. But this approach is slower because it will first create a list of objects with class "small" and then removes elements that does not have class "big"
<scripttype="text/javascript">
$(document).ready(function () {
$('.small').filter('.big').css('border', '5px solid red');
});
</script>

jQuery tutorial for beginners

4 comments:

  1. Learning a lot... Thank you for the tutorial...

    Reply Delete
  2. Thank you for the tutorial.

    Reply Delete
  3. Really very nice thank you for tutorials

    Reply Delete
  4. thank u sir for make such type of tutorial..

    Reply Delete

It would be great if you can help share these free resources

[フレーム]

Subscribe to: Post Comments (Atom)

AltStyle によって変換されたページ (->オリジナル) /