JavaScript Events 061827
JavaScript Events 061827
• <!DOCTYPE HTML>
• <html>
• <head>
• <title>Example: Javascript Inline Event Handlers </title>
• <style>
• p{font-family:Verdana;
• background:#FA8B7C;
• color:#fff;
• padding:10px; border:4px solid #555;}
• </style>
• </head>
• <body>
• <p onmouseover="this.style.background='#44c767';
• this.style.fontStyle='arial'">
• A system of morality which is based on relative
• emotional values is a mere illusion, a thoroughly vulgar
• conception which has nothing sound in it and nothing true.
• </p>
• <i>Hover over the text to see the effects.</i>
• </body>
• </html>
Javascript Events: HTML Inline Event Handling
• <!DOCTYPE HTML>
• <html>
• <head>
• <title>Example: Javascript Inline Event Handlers </title>
• <style type="text/css">
• p{font-family:Verdana;
• background:#FA8B7C;
• color:#fff;
• padding:10px; border:4px solid #555;}
• </style>
•
• </head>
• <body>
• <p onmouseover="this.style.background='#44c767';this.style.fontStyle='italic'"
• onmouseout="this.style.removeProperty('background');
• this.style.removeProperty('font-style')">
• A system of morality which is based on relative
• emotional values is a mere illusion, a thoroughly vulgar
• conception which has nothing sound in it and nothing true.
• </p>
• <i>Hover over the text to see the effects.</i>
• </body>
• </html>
Javascript Events: Event Handling Function
• <!DOCTYPE HTML>
• <html>
• <head>
• <title>Example: Javascript Event Handlers using a Function</title>
• <style type="text/css">
• p{font-family:Verdana;
• background:#FA8B7C;
• color:#fff;
• padding:10px; border:4px solid #555;}
• </style>
• <script>
• function mouseOverHandler(e){
• e.style.background = "#44c767";
• e.style.fontStyle="italic";
• }
•
• function mouseOutHandler(e){
• e.style.removeProperty('background');
• e.style.removeProperty('font-style');
• }
• </script>
• </head>
• <body>
• <p onmouseover="mouseOverHandler(this)" onmouseout="mouseOutHandler(this)">
• A system of morality which is based on relative
• emotional values is a mere illusion, a thoroughly vulgar
Javascript Mouse Events
• </head>
• <body>
• <p>
• A system of morality which is based on relative
• emotional values is a mere illusion, a thoroughly vulgar
• conception which has nothing sound in it and nothing true.
• </p>
• <p id="content">We must let go of the life we have planned,
• so as to accept the one that is waiting for us.</p>
•
•
• <i>Hover over the text to see the effects.</i>
• </body>
•
• <script>
•
• var item = document.getElementsByTagName("p");
•
• for(var i=0; i < item.length; i++){
• item[i].onmouseover = mouseEventHandler;
• item[i].onmouseout = mouseEventHandler;
• }
•
• function mouseEventHandler(e){
• if(e.type == "mouseover"){
• e.target.style.background = "#44c767";
• e.target.style.fontStyle = "italic";
• }
• else {
• e.target.style.removeProperty('font-style');
• e.target.style.removeProperty('background');
• }
• }
• </script>
• </html>
Javascript Keyboard Events
• </head>
• <body>
• <form>
• <p>
• <label for="name"> Name:
• <input autofocus id="name" name="name" /></label>
• </p>
• <p>
• <label for="nick"> Nickname:
• <input id="nick" name="nick" /></label>
• </p>
•
• <button type="submit">Submit</button>
• </form>
•
• <span id="output"></span>
•
• </body>
•
• <script>
• var items = document.getElementsByTagName("input");
• for (var i=0; i < items.length; i++){
• items[i].onkeyup = keyboardEventHandler;
• }
•
• function keyboardEventHandler(e){
• document.getElementById("output").innerHTML = "Key pressed is: " +
• e.keyCode + " Char:" + String.fromCharCode(e.keyCode);
• }
• </script>
• </html>
Javascript Window Events : onLoad and onUnLoad Event
• </head>
• <body>
• <form>
• <p>
• <label for="name"> Name:
• <input autofocus id="name" name="name" /></label>
• </p>
• <p>
• <label for="nick"> Nickname:
• <input id="nick" name="nick" /></label>
• </p>
•
• <button type="submit">Submit</button>
• </form>
•
• <span id="output"></span>
•
• </body>
•
• <script>
• var items = document.getElementsByTagName("input");
• for (var i=0; i < items.length; i++){
• items[i].onkeyup = keyboardEventHandler;
• }
•
• function keyboardEventHandler(e){
• document.getElementById("output").innerHTML = "Key pressed is: " +
• e.keyCode + " Char:" + String.fromCharCode(e.keyCode);
• }
• </script>
• </html>
Javascript Window Events : onfocus and onblur Event Handlers
• function focusEventHandler(e) {
• if (e.type == "focus") {
• e.target.style.backgroundColor = "#44c767";
• e.target.style.border = "3px solid #666";
• }
• else {
• e.target.style.removeProperty("background-color");
• e.target.style.removeProperty("border");
• }
• }
• </script>
• </body>