In order to use jQuery in a web page we must select the HTML elements of that web page to be able to work with them.
To select with jQuery the HTML elements of a web page we will use CSS selectors and jQuery self selectors.
In jQuery we can select the main HTML elements directly, with CSS selectors or modify these elements and their content with identifiers.
<body> → web content.
<head> → information about the document.
<div> → division of web content.
<a> → web links.
<strong> → bold text.
<br> → line breaks.
<H1>... <H6> → headings within the content.
<img> → image of a website.
Example: <div id="content_1">My content</div>
To start jQuery we must do the following inside a web page, just before the end of the body tag:
<script>
$(document).ready(function(){
});
</script>
Using $('*') inside a script we can select the whole html document:
<html>
<head></head>
<body>
.....
<script>
$(document).ready(function(){
$('*');
});
</script>
<!--JQuery Library-->
<script src="https://code.jquery.com/jquery-3.6.3.slim.min.js" integrity="sha256-ZwqZIVdD3iXNyGHbSYdsmWP//UBokj2FHAxKuSBKDSo=" crossorigin="anonymous"></script>
</body>
</html>
In the following example we have a div with id=part1. With jQuery we select that div and insert the html text "This is the Part 1".
<html>
<body>
<div id="part1"></div>
<script>
$(document).ready(function(){
$('#part1').html('This is the Part 1');
});
</script>
<!--JQuery Library-->
<script src="https://code.jquery.com/jquery-3.6.3.slim.min.js" integrity="sha256-ZwqZIVdD3iXNyGHbSYdsmWP//UBokj2FHAxKuSBKDSo=" crossorigin="anonymous"></script>
</body>
</html>
<script>
$(document).ready(function(){
$('table tr td:first-child');
});
</script>
<script>
$(document).ready(function(){
$("ul.my-class li:first-child").hide();
});
</script>
<script>
$(document).ready(function(){
$("ul.my-class li:nth-child(3)").show();
});
</script>
<script>
$(document).ready(function(){
$('td:last-of-type').hide();
});
</script>
<script>
$(document).ready(function(){
$('img:last-of-type').eq(6).hide();
});
</script>
Tips on SEO and Online Business
Next Articles
Previous Articles