Easy Learning Javascript Javascript For Beginners Guide by Yang Hu
Easy Learning Javascript Javascript For Beginners Guide by Yang Hu
YANG HU
ISBN : 9781093504781
CONTENTS
1. Javascript Basic Concept
1.1 Javascript HelloWorld
1.2 Variable
1.3 Variable and Type
2. Operational Operator
2.1 Arithmetic Operator
2.2 Function
2.3 Class
2.4 Type Conversion
2.5 DOM
2.6 Event
2.7 Create a Web Caculator
2.8 Assignment Operator
2.9 Relational Operator
2.10 If Conditional Statement
2.11 Logic Operator
3. Control Statement
3.1 Switch Statement
3.2 While Loop Statement
3.3 For Loop Statement
4. Array
4.1 One-Dimensional Array
4.2 Select All Check Box
4.3 Two-Dimensional Array
4.4 Find Dog Game
4.5 Secondary Linkage Drop-down
5. More Event
5.1 Login Web Page
5.2 MouseOver Thumbnail to Larger
6. DOM and Element Hierarchy
6.1 Element Hierarchy
6.2 Create Text Node
6.3 Delete Node
6.4 Replace Node
6.5 Add Contact Example
6.6 CSS Style Font
6.7 CSS Change Class Selector
6.8 CSS Overflow Expand and Close
6.9 CSS Floating Highlighting
6.10 Table Create Rows Columns
6.11 Delete Table Row Column
7. Timer
If you want to learn basic knowledge of HTML CSS: << Easy Learning
HTML CSS>> https://www.amazon.com/dp/B07QHRB23X
If you already have basic knowledge of HTML CSS, skip it, start an
exciting journey
Javascript HelloWorld
Javascript is a web page dynamic interactive
language that used in web development.
Result:
Variable
1.Create a file : Variable.html and open it in your
browser
// : single comments are not executed by javascript "":
string type
Result:
document.write ( "<br>" );
basket = "Orange" ;
document.write ( basket );
</script>
Run Result:
Variable and Type
Javascript is dynamic type language
1. Create a var.html file with Notepad and open it in
your browser to see the webpage
typeof(): find the type of a JavaScript variable
Result:
undefined number number boolean string string
Arithmetic Operator
1. Create a ArithmeticOperator.html file with Notepad
and open it in your browser Arithmetic Operator: Add
+, minus -, multiply *, divisible /, take modulo %
<script type = "text/javascript" >
var a = 1 ;
var b = 2 ;
var c = 3 ;
var d = 4 ;
document.write ( a + b ); // = 3
document.write ( "<br>" );
document.write ( a - b ); // = -1
document.write ( "<br>" );
document.write ( a * b ); // = 2
document.write ( "<br>" );
document.write ( b / a ); // = 2
document.write ( "<br>" );
document.write ( "<br>" );
Run Result:
6
8
function sub ( a , b ){
return a - b ;
}
function multiply ( a , b ){
return a * b ;
}
function divide ( a , b ){
return a / b ;
}
document.write ( "<br>" );
document.write ( "<br>" );
document.write ( "<br>" );
</script>
Class
this.add = function ( a , b ){
return a + b ;
}
this.sub = function ( a , b ){
return a - b ;
}
this.multiply = function ( a , b ){
return a * b ;
}
this.divide = function ( a , b ){
return a / b ;
}
}
</script>
Type Conversion
1. Create a TypeConversion.html
parseInt(): this is javascript default function can convert
string to integer.
<script type = "text/javascript" >
var a = "1" ;
var b = "2" ;
document.write ( "<br>" );
Result:
12
3
DOM
DOM: In Javascript every HTML element is a class object.
<input type= " text " />: html tag specifies an input field
where the user can enter data.
Assignment Operator
1. Create a AssignmentOperator.html file with
Notepad and open it in your browser
d=4;
document.write (++ d ); // = 5 increment 1 and then
print d document.write ( "<br>" );
d=4;
document.write ( d --); // = 4 print d and then
decrement 1
document.write ( "<br>" );
d=4;
document.write (-- d ); // = 3 decrement 1 and then
print d
var result = 10 ;
result = result + 1 ;
document.write ( result ); // = 11
document.write ( "<br>" );
result = 10 ;
result ++;
document.write ( result ); // = 11
document.write ( "<br>" );
result = 10 ;
result += 1 ;
document.write ( result ); // = 11
document.write ( "<br>" );
</script>
Relational Operator
1. Create a RelationalOperator.html file with Notepad
and open it in your browser
Relational operators only two value: true, false
document.write ( 1 == 1 ); // true
document.write ( "<br>" );
document.write ( 1 != 2 ); // true
</script>
If Conditional Statement
If statement 1. if (expression){statement}
2. if (expression) { statement } else { statement }
3. if (expression) {statement } else if { statement }
Logic Operator
1. Create a LogicOperator.html file with Notepad and
open it in your browser
Logical Operator: and &&, or ||, not !
1. && returns true if both sides of the operation are true,
otherwise false 2. || The result is false when both sides of
the operation are false, otherwise true; 3. ! if returns true,
the result is false, otherwise is true
2. Create a LogicOperator2.html
document.write ( "--------------" )
document.write ( 2 > 1 || 3 > 4 ) // = true
document.write ( 2 > 1 || 3 > 4 ) // = true
document.write ( 1 > 2 || 3 > 4 ) // = false
</script>
Result:
tax amount=2000
2. Create a WebTax.html
</script>
Switch Statement
1. Create a switch.html file with Notepad and open it
in your browser
Input a number 1 , 2, 3
1 : Pay by Visa Card
2 : Pay by Master Card
3: Pay by Paypal
Otherwise Pay by face to face
var num = 1 ;
switch ( num ) {
case 1 :
document.write ( "Pay by Visa Card" );
break ; // terminate the code to continue execution
case 2 :
document.write ( "Pay by Master Card" );
break ;
case 3 :
document.write ( "Pay by Paypal" );
break ;
default :
document.write ( "Pay by face to face" );
}
</script>
Result:
Pay by Visa Card
2. Create a WebPay.html
switch ( num ) {
case 1 :
resultObj.innerHTML = "Pay by Visa Card" ;
break ;
case 2 :
resultObj.innerHTML = "Pay by Master Card" ;
break ;
case 3 :
resultObj.innerHTML = "Pay by Paypal" ;
break ;
default :
resultObj.innerHTML = "Pay by face to face" ;
}
}
</script>
var i = 0 ;
while ( i < 10 )
{
document.write ( i + "," );
i ++;
}
</script>
Result:
0,1,2,3,4,5,6,7,8,9,
Result:
0,1,2,3,4,5,6,7,8,9,
One-Dimensional Array
</script>
Result:
90
50
60
var scores = [ 90 , 70 , 50 , 80 , 60 , 85 ];
</script>
Result:
90,70,50,80,60,85,
</script>
Result:
90,70,50,80,60,85,
Two-Dimensional Array
</script>
Result:
10
30
60
var arrs =[
[ 10 , 20 , 30 ],
[ 40 , 50 , 60 ],
[ 70 , 80 , 90 ]
];
// i: row index, j: column index for ( var i = 0 ; i <
arrs.length ; i ++) {
for ( var j = 0 ; j < arrs [ i ]. length ; j ++) {
document.write ( arrs [ i ][ j ]);
document.write ( " " );
}
document.write ( "<br>" );
}
</script>
<style> input{
width:40px; height:40px; }
</style>
<span id = "result" ></span>
<script type = "text/javascript" >
var maps =[
[ 1 , 1 , 1 , 1 ],
[ 1 , 1 , 1 , 1 ],
[ 1 , 2 , 1 , 1 ],
[1,1,1,1]
]; var mapHTML = "" ;
for ( var i = 0 ; i < maps.length ; i ++) {
for ( var j = 0 ; j < maps [ i ]. length ; j ++) {
mapHTML += "<input type='button' value=' ' />" ;
}
mapHTML += "<br>" ;
}
document.getElementById ( "result" ). innerHTML =
mapHTML ;
</script>
1: *
2: Dog
<style> input{
width:40px; height:40px; }
</style>
<span id = "result" ></span>
<script type = "text/javascript" >
var maps =[
[ 1 , 1 , 1 , 1 ],
[ 1 , 1 , 1 , 1 ],
[ 1 , 2 , 1 , 1 ],
[1,1,1,1]
];
var mapHTML = "" ;
for ( var i = 0 ; i < maps.length ; i ++) {
for ( var j = 0 ; j < maps [ i ]. length ; j ++) {
mapHTML += "<input type='button' value=' '
onClick='doButtonClick(this," + maps [ i ][ j ]+ ")' />" ;
}
mapHTML += "<br>" ;
}
document.getElementById ( "result" ). innerHTML =
mapHTML ;
Result:
9, 3, 9, 0, 4, 1, 0, 3, 4, 5,
1: *
2: Dog
<style> input{
width:40px; height:40px; }
</style>
<span id = "result" ></span>
<script type = "text/javascript" >
var maps =[
[ 0 , 0 , 0 , 0 ],
[ 0 , 0 , 0 , 0 ],
[ 0 , 0 , 0 , 0 ],
[0,0,0,0]
];
for ( var i = 0 ; i < maps.length ; i ++) {
for ( var j = 0 ; j < maps [ i ]. length ; j ++) {
var num = Math.floor ( Math.random ()* 2 )+ 1
maps [ i ][ j ] = num ;
}
}
window.onload = function ()
{
var countryObj = document.getElementById ( "country"
);
</script>
window.onload = function ()
{
var countryObj = document.getElementById ( "country"
);
</script>
window.onload = function ()
{
var country = document.getElementById ( "country" );
for ( var i = 0 ; i < countryArray.length ; i ++)
{
var option = document.createElement ( "option" );
option.text = countryArray [ i ];
option.value = countryArray [ i ];
country.options [ country.options.length ]= option ;
}
}
if ( usernameObj.value == "" )
{
alert ( "Please input username !" );
return ;
}
if ( pwdObj.value == "" )
{
alert ( "Please input password !" );
return ;
}
alert ( "login successfull !" );
}
</script>
function checkLogin ()
{
var usernameObj = document.getElementById (
"username" );
var pwdObj = document.getElementById ( "pwd" );
if ( usernameObj.value == "" )
{
alert ( "Please input username !" );
return ;
}
if ( pwdObj.value == "" )
{
alert ( "Please input password !" );
return ;
}
MouseOver Thumbnail to
Larger
1. Create a ThumbnailLarger.html file with Notepad
and open it in your browser
<img>: tag defines an image in an HTML page.
Element Hierarchy
1. Change ElementHierarchy.html to get all child
nodes.
function doGetChildNodes ()
{
var div = document.getElementById ( "div" );
var childNodes = div.childNodes ;
for ( var i = 0 ; i < childNodes.length ; i ++)
{
if ( childNodes [ i ]. nodeType == 1 )
{
document.write ( childNodes [ i ]. nodeName + " , " );
document.write ( childNodes [ i ]. nodeType + " , " );
document.write ( childNodes [ i ]. innerHTML + "<br>"
);
}
}
}
</script>
function doGetSliding ()
{
var bookObj = document.getElementById ( "book" );
document.write ( bookObj.previousSibling.innerHTML + "
<br>" );
document.write ( bookObj.nextSibling.innerHTML );
}
</script>
<style> div{
width:200px; height:100px; border:1px solid
#ff0000; }
</style>
<div id = "div" ></div> <input type = "button" value =
"Create Text Node" onclick = "doCreateTextNode()"
/>
<script type = "text/javascript" >
function doCreateTextNode ()
{
var divObj = document.getElementById ( "div" );
var newNode = document.createTextNode ( "Good
Dreams" );
divObj.appendChild ( newNode );
}
</script>
function doCreateElement ()
{
var divObj = document.getElementById ( "div" );
var buttonElement = document.createElement ( "input"
);
buttonElement.type = "button" ;
buttonElement.value = "button" ;
divObj.appendChild ( buttonElement );
}
</script>
Delete Node
1. Create a DeleteNode.html file with Notepad and
open it in your browser
obj . removeChild: removes a specified child node
<style> div{
width:200px; height:250px; border:1px solid
#ff0000; }
</style>
<div id = "div1" > <img id = "image" src =
"python.jpg" /> </div> <br> <input type = "button"
value = "Remove Child" onclick = "doRemoveChild()"
/>
<script type = "text/javascript" >
function doRemoveChild ()
{
var div = document.getElementById ( "div1" );
var img = document.getElementById ( "image" );
div.removeChild ( img );
}
</script>
Replace Node
1. Create a ReplaceNode.html file with Notepad and
open it in your browser
<style> #div1{
width:200px; height:200px; border:1px solid
#ff0000; }
</style>
<div id = "div1" > Everyone has inside them a piece
of good news. </div> <input type = "button" value =
"Replace Child Node" onclick = "doReplaceChild()" />
<script type = "text/javascript" >
function doReplaceChild ()
{
var imgNode = document.createElement ( "img" );
imgNode.src = "python.jpg" ;
var div1 = document.getElementById ( "div1" );
div1.replaceChild ( imgNode , div1.childNodes [ 0 ]);
}
</script>
<style> #div{
border:1px solid #ff0000; width:550px; }
</style>
<a href = "javascript:void(0)" onclick =
"addContact()" > Add Contact </a> <div id = "div" >
<div id = "contact" > Name: <input type = "text" name
= "user" /> Telephone: <input type = "text" name =
"telephone" /> <a href = "javascript:void(0)" onclick
= "deleteNode(this)" > Delete </a> </div> </div>
<script type = "text/javascript" >
function addContact ()
{
var contact = document.getElementById ( "contact" );
var newNone = contact.cloneNode ( true );
</script>
CSS Style Font
function changeFont ()
{
var div = document.getElementById ( "div" );
if ( div.className == "smallFont" )
{
div.className = "bigFont" ;
}
else {
div.className = "smallFont" ;
}
}
</script>
.close{
display :none ; }
#div{
width:300px; height:30px; border:1px solid
#ff0000; text-align:right; }
#div2{
width:300px; border:1px solid #00ff00; }
</style>
</script>
.down{
background-color: #0000ff; }
table{
border:1px solid #eeeeee; width:400px; border-
collapse: collapse; }
table th{
border:1px solid #eeeeee; background-color:
#cccccc; }
table td{
border:1px solid #eeeeee; }
</style>
</script>
function doCreateRow ()
{
var rowNum = parseInt ( document.getElementById (
"row" ). value );
var colNum = parseInt ( document.getElementById (
"col" ). value );
</script>
.tableClass td{
border:1px solid #ff0000; }
</style> <input type = "button" value = "Create Table"
onclick = "doCreateTable()" /> <input type = "button"
value = "Create Row" onclick = "doCreateRow()" />
<br> <input type = "button" value = "Delete Row"
onclick = "doDeleteRow()" /> <input type = "button"
value = "Delete Col" onclick = "doDeleteCol()" /> <br>
Rows: <input type = "text" value = "" id = "row" />
Columns: <input type = "text" value = "" id = "col" />
<br> <br> <div id = "div" > </div>
function doCreateRow ()
{
var rowNum = parseInt ( document.getElementById (
"row" ). value );
var colNum = parseInt ( document.getElementById (
"col" ). value );
function doDeleteRow ()
{
var rowNum = parseInt ( document.getElementById (
"row" ). value );
table.deleteRow ( rowNum );
}
function doDeleteCol ()
{
var colNum = parseInt ( document.getElementById (
"col" ). value );
var rows = table.rows ;
for ( var i = 0 ; i < rows.length ; i ++)
{
rows [ i ]. deleteCell ( colNum );
}
}
</script>
Timer
1. Create a Timer.html file with Notepad and open it
in your browser
getFullYear(): Get the year as a four digit number (yyyy)
getMonth(): Get the month as a number (0-11) getDate():
Get the day as a number (1-31) getHours(): Get the hour
(0-23) getMinutes(): Get the minute (0-59)
getSeconds(): Get the second (0-59)
getTime(): Get the time (milliseconds since January 1,
1970) setInterval(): continue calling the function until
clearInterval() is called.
dateDiv.innerHTML = dateString ;
}
https://www.amazon.com/dp/B08D863KFY
http://www.amazon.com/review/create-review?
&asin=1093504781