Web-fejlesztés 2.
Visnovitz Márton
egyetemi tanársegéd
visnovitz.marton@inf.elte.hu
@elte-fi/courses-tt-webprog
elem.innerHTML
, elem.innerText
, elem.value
document.querySelector("css selector")
document.querySelectorAll("css selector")
innerHTML
)elem.addEventListener(type, handler)
<form>
Name: <input type="text" id="name"> <br>
<input type="button" value="Say hello!" id="hello">
<br>
<span id="output"></span>
</form>
function greet(name) {
return `Hello ${name}!`;
}
const input = document.querySelector("#name");
const output = document.querySelector("#output");
const hello = document.querySelector("#hello");
function handleHelloClick() {
const name = input.value;
const greeting = greet(name);
output.innerHTML = greeting;
}
hello.addEventListener("click", handleHelloClick);
Dinamikusan generált felhasználói felület
innerHTML
tulajdonság írása<div id="output"></div>
<script>
const greeting = "<h1>Hello <em>World</em></h1>";
const output = document.querySelector("#output");
output.innerHTML = greeting;
</script>
// Rövid statikus szöveg megadása
const html1 = `<h1>Hello there!</h1>`;
// Többsoros statikus szöveg megadása
const html2 = `
<div>
<p>No, <strong>I</strong> am your father!</p>
</div>
`;
// Változók behelyettesítése
const callsign = 'Red 5';
const html3 = `${callsign}, standing by.`;
// Tömbök kiírása leképezéssel
const callsigns = ["Red 10", "Red 7", "Red 3", "Red 6", "Red 9"];
const html4 = `
<p>All wings, report in.</p>
<ul>
${callsigns.map(callsign => `
<li>${callsign}, standing by.</li>
`).join("")}
</ul>
`;
// Feltételes kiírás (elágazás)
const chanceOfSuccess = 0.4;
const html5 = `
<span>
I have a
${chanceOfSuccess < 0.5 ? "bad" : "good"}
feeling about this.
</span>
`;
// Függvényekkel
function hanDescription(properties) {
return `
You
<ul>
${properties.map(hanProperty).join("")}
</ul>
Nerf Herder!
`;
}
function hanProperty(property) {
return `<li>${property}</li>`;
}
const hanProperties = [
"stuck up",
"half witted",
"scruffy looking"
];
const html6 = hanDescription(hanProperties);
Programozottan:
document.createElement(elementName)
parent.appendChild(childElement)
parent.insertBefore(newChildElement, ref)
ref.insertAdjacentElement(position, newElement)
<body>
<ul>
<li>First</li>
<li>Second</li>
✒> <✒
<li>Third</li>
</ul>
✒> <✒
</body>
const p = document.createElement("p");
document.body.appendChild(p);
const newLi = document.createElement("li");
const ul = document.querySelector("ul");
const refLi =
ul.querySelector("li:nth-of-type(3)");
ul.insertBefore(newLi, refLi);
<div class="rodian bounty-hunter" style="bottom: 72in">
Greedo
</div>
HTML elem style
tulajdonságának olvasása/írása
CSS stílustulajdonság | style objektum tulajdonsága |
---|---|
left |
left |
background-color |
backgroundColor |
border-bottom-width |
borderBottomWidth |
border-top-left-radius |
borderTopLeftRadius |
<div style="position: absolute" id="movingElement"></div>
<script>
document.querySelector("#movingElement").style.top = "25px";
document.querySelector("#movingElement").style.left = "42px";
</script>
elem.style
: CSSStyleDeclaration
objektumstyle
attribútumon keresztül voltak megadva;<style>
.box {
position: absolute;
width: 100px; height: 100px;
}
</style>
<div class="box" style="left: 20px"></div>
const box = document.querySelector("div");
box.style.top = "30px";
box.style.top; // "30px" <-- JS
box.style.left; // "20px" <-- style attribute
box.style.width; // ""
box.style.position; // ""
getComputedStyle(elem)
border
, background
, stb.) nem érhető el, csak az elemi tulajdonságok.<style>
.box {
position: absolute;
width: 100px; height: 100px;
}
</style>
<div class="box" style="left: 20px"></div>
const box = document.querySelector("div");
box.top = "30px";
const computedStyle = window.getComputedStyle(box);
computedStyle.top // "30px"
computedStyle.left // "20px"
computedStyle.width // "100px"
computedStyle.position // "absolute"
classList
tulajdonság
add(className)
remove(className)
toggle(className)
contains(className)
add
, remove
, toggle
, contains
<div class="red green blue">
const div = document.querySelector('div');
div.classList.remove("green");
div.classList.add("pink");
// váltogatás
div.classList.toggle("pink");
// feltételes megjelenítés
div.classList.toggle("pink", i < 10);
// van-e adott stílusosztály
div.classList.contains("red"); // true
// több hozzáadása egyszerre
div.classList.add("orange", "yellow");
Interaktív programok
// Egyszerűbb esetekben
const button = document.querySelector("button");
function handleButtonClick() {
// mi történjen kattintáskor
}
button.addEventListener("click", handleButtonClick);
click
: egérkattintásmousemove
: egérmozgatásmousedown
: egér gombjának lenyomásamouseup
: egér gombjának felengedéseinput
: input mező értékének megváltozásakeydown
: billentyűzet gombjának lenyomásakeyup
: billentyűzet gombjának felengedésekeypress
: billentyűzet gombjának megnyomásasubmit
: űrlap elküldésescroll
: görgetés az oldalonfunction handleEvent() {
// Read
// Process
// Write
}
<form>
Name: <input type="text" id="name"> <br>
<input type="button" value="Say hello!" id="hello">
<br>
<span id="output"></span>
</form>
function greet(name) {
return `Hello ${name}!`;
}
const input = document.querySelector('#name');
const output = document.querySelector('#output');
const hello = document.querySelector('#hello');
function handleHelloClick() {
const name = input.value;
const greeting = greet(name);
output.innerHTML = greeting;
}
hello.addEventListener('click', handleHelloClick);
// Egy elem egy eseményéhez több eseménykezelő függvény
button.addEventListener("click", handleButtonClick1);
button.addEventListener("click", handleButtonClick2);
// Több eseményhez ugyanaz az eseménykezelő függvény
button1.addEventListener("click", handleButtonClick);
button2.addEventListener("click", handleButtonClick);
Mi van akkor ha ugyanazt az eseménykezelőt szeretnék sok elemhez hozzárendelni?
1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 |
function handleCellClick() { /* ... */ }
const cells = document.querySelectorAll("table td");
for (let cell of cells) {
cell.addEventListener("click", handleCellClick);
}
Probléma: Mi a helyzet, ha új elemek jönnek létre?
event
)event.target
)this
)forrás → szülő → szülő → ... → body → html → document → window
e.stopPropagation()
forrás → szülő → szülő → ... → body → html → document → window
const table = document.querySelector("table");
function handleCellClick(event) {
// Run only if the current target is a `td`
if (!✒>event.target<✒.matches("td"));
/* ... */
}
table.addEventListener("click", handleTableClick);
elem.style
getComputedStyle
classList
elem.addEventListener(type, handler)