Universal (*)
In this chapter you will learn:
- Description and Syntax for Universal selector
- Examples for Universal selector
- How to select all elements under a certain element
- How to select all elements
Description and Syntax
$('*')
selects all elements.
Examples
$('*')
all elements in the document$('p > *')
all elements that are children of a paragraph element
<!DOCTYPE html> <!--from ja v a 2 s. c om-->
<html>
<head>
<script src="http://java2s.com/style/jquery-1.8.0.min.js">
</script>
<script>
$(document).ready(function(){
var allElements = $("*");
document.writeln(allElements.length);
});
</script>
<body>
<div id="main">
<ul id="myList">
<li>A</li>
<li>B</li>
</ul>
</div>
</body>
</html>
Select all elements under a certain element
The following code selects all elements under a certain id.
<html><!--from j a v a2s . co m-->
<head>
<script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#main > *").css("border", "3px solid red");
});
</script>
</head>
<body>
<body>
<span id="main">
<div>asdf</div>
<button>Child</button>
<span>A Span</span>
</span>
</body>
</html>
Match all elements
The following code selects every element (including head
, body
, etc).
<html><!-- jav a 2 s . com-->
<head>
<script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("*").css("border","3px solid red");
});
</script>
</head>
<body>
<body>
<div>A div</div>
<button>0</button>
<button>1</button>
<button>2</button>
<button>3</button>
<span></span>
</body>
</html>
Next chapter...
What you will learn in the next chapter: