restructured

Code Samples of CH8-CH11 was restructured
This commit is contained in:
Diablo_xDSD
2024-01-19 17:37:31 +03:00
parent f20632ab9e
commit ea15a3c75d
28 changed files with 0 additions and 0 deletions
@@ -0,0 +1,11 @@
<html>
<body>
<h1 style="color:pink;">Just an example</h1>
<div id="one" class="example">Hi!</div>
<div id="two" class="example">Hi!</div>
<div id="three" class="something">Hi!</div>
</body>
<script>
console.log(document.getElementById("two"));
</script>
</html>
-30
View File
@@ -1,30 +0,0 @@
<html>
<style>
div {
background-color: purple;
width: 100px;
height: 100px;
position: absolute;
}
</style>
<body>
<button onclick="toTheRight()">Go right</button>
<div id="block"></div>
<script>
function toTheRight() {
let b = document.getElementById("block");
let x = 0;
setInterval(function () {
if (x === 600) {
clearInterval();
} else {
x++;
b.style.left = x + "px";
}
}, 2);
}
</script>
</body>
</html>
@@ -0,0 +1,26 @@
<html>
<body>
<script>
function changeAttr(){
let el = document.getElementById("shape");
el.setAttribute("style", "background-color:red;border:1px solid black");
el.setAttribute("id", "new");
el.setAttribute("class", "circle");
}
</script>
<style>
div {
height: 100px;
width: 100px;
background-color: yellow;
}
.circle {
border-radius: 50%;
}
</style>
<div id="shape" class="square"></div>
<button onclick="changeAttr()">Change attributes...</button>
</body>
</html>
+27
View File
@@ -0,0 +1,27 @@
<html>
<body>
<script>
function disappear(){
document.getElementById("shape").classList.add("hide");
}
</script>
<style>
.hide {
display: none;
}
.square {
height: 100px;
width: 100px;
background-color: yellow;
}
.square.blue {
background-color: blue;
}
</style>
<div id="shape" class="square blue"></div>
<button onclick="disappear()">Disappear!</button>
</body>
</html>
@@ -0,0 +1,23 @@
<html>
<body>
<script>
function change(){
document.getElementById("shape").classList.remove("blue");
}
</script>
<style>
.square {
height: 100px;
width: 100px;
background-color: yellow;
}
.square.blue {
background-color: blue;
}
</style>
<div id="shape" class="square blue"></div>
<button onclick="change()">Change!</button>
</body>
</html>
@@ -0,0 +1,23 @@
<html>
<body>
<script>
function changeVisibility(){
document.getElementById("shape").classList.toggle("hide");
}
</script>
<style>
.hide {
display: none;
}
.square {
height: 100px;
width: 100px;
background-color: yellow;
}
</style>
<div id="shape" class="square"></div>
<button onclick="changeVisibility()">Magic!</button>
</body>
</html>
+23
View File
@@ -0,0 +1,23 @@
<!-- <html>
<body>
<div id="one" onclick="alert('Auch! Stop it!')">Don't click here!</div>
</body>
</html>
<html>
<body>
<script>
function stop(){
alert("Auch! Stop it!");
}
</script>
<div id="one" onclick="stop()">Don't click here!</div>
</body>
</html> -->
<html>
<body>
<div id="one">Don't click here!</div>
</body>
</html>
+27
View File
@@ -0,0 +1,27 @@
<html>
<body>
<script>
function rainbowify(){
let divs = document.getElementsByTagName("div");
for(let i = 0; i < divs.length; i++) {
divs[i].style.backgroundColor = divs[i].id;
}
}
</script>
<style>
div {
height: 30px;
width: 30px;
background-color: white;
}
</style>
<div id="red"></div>
<div id="orange"></div>
<div id="yellow"></div>
<div id="green"></div>
<div id="blue"></div>
<div id="indigo"></div>
<div id="violet"></div>
<button onclick="rainbowify()">Make me a rainbow</button>
</body>
</html>
+33
View File
@@ -0,0 +1,33 @@
<html>
<body>
<style>
div {
border: 1px solid black;
margin-left: 5px;
}
</style>
<div id="message">Bubbling events</div>
<div id="output">
1
<div>
2
<div>
3
<div>
4
<div>5</div>
</div>
</div>
</div>
</div>
<script>
function bup() {
console.log(this.innerText);
}
let divs = document.getElementsByTagName("div");
for (let i = 0; i < divs.length; i++) {
divs[i].addEventListener("click", bup);
}
</script>
</body>
</html>
@@ -1,46 +0,0 @@
<html>
<head>
<style>
.box {
height: 200px;
width: 200px;
padding: 20px;
margin: 0 50px;
display: inline-block;
border: 1px solid black;
}
#dragme {
background-color: red;
}
</style>
</head>
<body>
<div class="box" ondrop="dDrop()" ondragover="nDrop()">
1
<div id="dragme" ondragstart="dStart()" draggable="true">
Drag Me Please!
</div>
</div>
<div class="box" ondrop="dDrop()" ondragover="nDrop()">2</div>
<script>
let holderItem;
function dStart() {
holderItem = event.target;
}
function nDrop() {
event.preventDefault();
}
function dDrop() {
// event.preventDefault();
if (event.target.className == "box") {
event.target.appendChild(holderItem);
}
}
</script>
</body>
</html>
@@ -0,0 +1,16 @@
<html>
<body>
<script>
window.onload = function() {
document.getElementById("square").addEventListener("click", changeColor);
}
function changeColor(){
let red = Math.floor(Math.random() * 256);
let green = Math.floor(Math.random() * 256);
let blue = Math.floor(Math.random() * 256);
this.style.backgroundColor = `rgb(${red}, ${green}, ${blue})`;
}
</script>
<div id="square" style="width:100px;height:100px;background-color:grey;">Click for magic</div>
</body>
</html>
@@ -1,10 +0,0 @@
<html>
<body>
<button type="button" onclick="triggerSomething()">Click</button>
<script>
function triggerSomething() {
console.dir(event.target);
}
</script>
</body>
</html>
@@ -1,21 +0,0 @@
<html>
<body>
<div id="welcome">Hi there!</div>
<form>
<input type="text" name="firstname" placeholder="First name" />
<input type="text" name="lastname" placeholder="Last name" />
<input type="button" onclick="sendInfo()" value="Submit" />
</form>
<script>
function sendInfo() {
console.dir(event);
let p = event.target.parentElement;
message("Welcome " + p.firstname.value + " " + p.lastname.value);
}
function message(m) {
document.getElementById("welcome").innerHTML = m;
}
</script>
</body>
</html>
@@ -1,8 +0,0 @@
<html>
<body>
<form action="anotherpage.html" method="get" onsubmit="doStuff()">
<input type="text" placeholder="name" name="name" />
<input type="submit" value="send" />
</form>
</body>
</html>
@@ -1,33 +0,0 @@
<html>
<body>
<div id="wrapper"></div>
<form action="anotherpage.html" method="get" onsubmit="return valForm()">
<input type="text" id="firstName" name="firstName" placeholder="First name" />
<input type="text" id="lastName" name="lastName" placeholder="Last name" />
<input type="text" id="age" name="age" placeholder="Age" />
<input type="submit" value="submit" />
</form>
<script>
function valForm() {
var p = event.target.children;
if (p.firstName.value == "") {
message("Need a first name!!");
return false;
}
if (p.lastName.value == "") {
message("Need a last name!!");
return false;
}
if (p.age.value == "") {
message("Need an age!!");
return false;
}
return true;
}
function message(m) {
document.getElementById("wrapper").innerHTML = m;
}
</script>
</body>
</html>
@@ -1,23 +0,0 @@
<html>
<body>
<body>
<div id="wrapper">JavaScript is fun!</div>
<input type="text" name="myNum1" onkeypress="numCheck()">
<input type="text" name="myNum2" onkeypress="numCheck2()">
<script>
function numCheck() {
message("Number: " + !isNaN(event.key));
return !isNaN(event.key);
}
function numCheck2() {
message("Not a number: " + isNaN(event.key));
return isNaN(event.key);
}
function message(m) {
document.getElementById('wrapper').innerHTML = m;
}
</script>
</body>
</html>
@@ -1,23 +0,0 @@
<html>
<body>
<body>
<div id="wrapper">JavaScript is fun!</div>
<input type="text" name="myNum1" onkeypress="return numCheck()" onpaste="return false">
<input type="text" name="myNum2" onkeypress="return numCheck2()" onpaste="return false">
<script>
function numCheck() {
message(!isNaN(event.key));
return !isNaN(event.key);
}
function numCheck2() {
message(isNaN(event.key));
return isNaN(event.key);
}
function message(m) {
document.getElementById('wrapper').innerHTML = m;
}
</script>
</body>
</html>
@@ -1,11 +0,0 @@
<html>
<body>
<div id="divvy" onmouseup="changeColor()" style="width: 100px; height: 100px; background-color: pink;">
<script>
function changeColor() {
document.getElementById("divvy").style.backgroundColor = "blue";
}
</script>
</body>
</html>
@@ -1,20 +0,0 @@
<html>
<body>
<div id="divvy" style="width: 100px; height: 100px; background-color: pink;">
<script>
window.onload = function donenow() {
console.log("hi");
document.getElementById("divvy").addEventListener("mousedown", function() { changeColor(this, "green"); });
document.getElementById("divvy").addEventListener("mouseup", function() { changeColor(this, "yellow"); });
document.getElementById("divvy").addEventListener("dblclick", function() { changeColor(this, "black"); });
document.getElementById("divvy").addEventListener("mouseout", function() { changeColor(this, "blue"); });
}
console.log("hi2");
function changeColor(el, color) {
el.style.backgroundColor = color;
}
</script>
</body>
</html>
@@ -0,0 +1,12 @@
<html>
<body>
<script>
function addRandomNumber(){
let el = document.createElement("p");
el.innerText = Math.floor(Math.random() * 100);
document.body.appendChild(el);
}
</script>
<button onclick="addRandomNumber()">Add a number</button>
</body>
</html>
@@ -1,29 +0,0 @@
<html>
<body>
<div id="welcome">Hi there!</div>
<form>
<input type="text" name="firstname" placeholder="First name" onchange="logEvent()" />
<input type="text" name="lastname" placeholder="Last name" onblur="logEvent()" />
<input type="button" onclick="sendInfo()" value="Submit" />
</form>
<script>
function logEvent() {
let p = event.target;
if (p.name == "firstname") {
message("First Name Changed to " + p.value);
} else {
message("Last Name Changed to " + p.value);
}
}
function sendInfo() {
let p = event.target.parentElement;
message("Welcome " + p.firstname.value + " " + p.lastname.value);
}
function message(m) {
document.getElementById("welcome").innerHTML = m;
}
</script>
</body>
</html>
@@ -0,0 +1,8 @@
<html>
<body>
<h1>Welcome page</h1>
<p id="greeting">
Hi!
</p>
</body>
</html>
@@ -1,13 +0,0 @@
<html>
<body>
<p id="unique" onclick="magic()">Click here for magic!</p>
<script>
document.getElementById("unique").onclick = function() { magic() };
document.getElementById("unique").addEventListener("click", magic);
document.getElementById("unique").addEventListener("click", function() { magic(arg1, arg2) });
</script>
</body>
</html>
+10
View File
@@ -0,0 +1,10 @@
<html>
<body>
<script>
function reveal(el){
console.log(el.parentElement);
}
</script>
<button onclick="reveal(this)">Click here!</button>
</body>
</html>
@@ -0,0 +1,16 @@
<html>
<body>
<script>
function toggleDisplay(){
let p = document.getElementById("magic");
if(p.style.display === "none") {
p.style.display = "block";
} else {
p.style.display = "none";
}
}
</script>
<p id="magic">I might disappear and appear.</p>
<button onclick="toggleDisplay()">Magic!</button>
</body>
</html>
@@ -0,0 +1,19 @@
<html>
<body>
<h1>Let's find the treasure</h1>
<div id="forest">
<div id="tree1">
<div id="squirrel"></div>
<div id="flower"></div>
</div>
<div id="tree2">
<div id="shrubbery">
<div id="treasure"></div>
</div>
<div id="mushroom">
<div id="bug"></div>
</div>
</div>
</div>
</body>
</html>