Simple JavaScript applications and javascript applications
Simple JavaScript Application
I. Introduction
This note is a summary and key record of Chapter 4th of the JavaScript authoritative guide. It is not a reproduction of book instances. Most of the examples in the book are stored on github. If you are interested, you can read this book or the Code on github. The github address is stored in the JavaScript directory.
Ii. JavaScript Application steps
1. Write JavaScript scripts
2. Reference in the page:
A) You can directly use the <script> tag on the page to add JavaScript-related content.
B) You can add the script to one or more files ending with JavaScript and introduce the script on the desired page.
Iii. instance description
When a webpage contains multiple links and no links are clicked, a default image and default description are displayed at the bottom of the page. When a specific link is clicked, the default image at the bottom is replaced. link to the image, and display the description of the link at the specified position.
Iv. Key Points of instance implementation
1. Obtain the herf and title attributes of the link.
2. Add a click event for all links.
3. Block the default link behavior.
2. Modify the src of the default image label and the text content in the description label.
V. Related methods and Solutions
1. prevent a's default behavior:
<li><a onclick="switchPicture(this); return false" href="../picture/chenhong.jpg" title="Chen hong">Chen hong</a></li>
2. Related Methods
function switchPicture (whichPicture) {var source = whichPicture.href;var placeholder = document.getElementById('placeholder');placeholder.setAttribute('src', source);document.getElementById('description').firstChild.nodeValue = whichPicture.getAttribute('title');}
Vi. Supplement
Effect:
ShowPicture. js:
function switchPicture (whichPicture) {var source = whichPicture.href;var placeholder = document.getElementById('placeholder');placeholder.setAttribute('src', source);document.getElementById('description').firstChild.nodeValue = whichPicture.getAttribute('title');}/* function countBodyChildren () { alert(document.getElementsByTagName('body')[0].childNodes.length); } window.onload = countBodyChildren;*/
ShowPicture.html:
<!DOCTYPE html>
ShowPicture.css:
body {font-family: "Helvetica", "Arial", serif;color: #333;background-color: #ccc;margin: 1em 10%;}h1 {color: #333;background-color: transparent;}a {color: #c60;background-color: transparent;font-weight: bold;text-decoration: none;}ul {padding: 0;}li {float: left;padding: 1em;list-style: none;}img {display: block;clear: both;}