jQuery selectors and events 1
Sat Nov 16 2024 01:58:10 GMT+0000 (Coordinated Universal Time)
Saved by
@signup1
//index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Selectors and Events Example</title>
<link rel="stylesheet" href="styles.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<button id="myButton" class="btn">Click Me</button>
<div id="hoverDiv" class="box">Hover Over Me</div>
<p class="para">This is a paragraph. Click it to change text.</p>
<script src="script.js"></script>
</body>
</html>
/* Basic Styles */
body {
font-family: Arial, sans-serif;
text-align: center;
padding: 50px;
}
#myButton {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
#hoverDiv {
width: 200px;
height: 100px;
margin-top: 20px;
background-color: lightgray;
display: inline-block;
line-height: 100px;
cursor: pointer;
}
.para {
margin-top: 20px;
font-size: 18px;
cursor: pointer;
}
//jQuery(Script.js)
$(document).ready(function() {
// Click event for the button
$('#myButton').click(function() {
alert('Button was clicked!');
});
// Hover event for the div
$('#hoverDiv').hover(
function() { // Mouse enter
$(this).css('background-color', 'yellow');
},
function() { // Mouse leave
$(this).css('background-color', 'lightgray');
}
);
// Click event for the paragraph
$('.para').click(function() {
$(this).text('The text has been changed!');
});
});
content_copyCOPY
Comments