Practical No 8css
Practical No 8css
8:
Create a webpage to implement Form Events. Part-II. Mouse
Event:
Mouse Event are used to capture the interaction made by the user by using mouse.
Attribute Value Description
onclick script Fires on a mouse click on the element
ondblclick script Fires on a mouse double-click on the element
onmousedown script Fires when a mouse button is pressed down on an element
onmousemove script Fires when the mouse pointer is moving while it is over an
element
onmouseout script Fires when the mouse pointer moves out of an element
onmouseover script Fires when the mouse pointer moves over an element
onmouseup script Fires when a mouse button is released over an element
onmousewheel script Deprecated. Use the onwheel attribute instead
onwheel script Fires when the mouse wheel rolls up or down over an element
3. Mousemove:
The mousemove event works fine, but you should be aware that it may take quite some
system time
to process all mousemove events. If the user moves the mouse one pixel, the mousemove
event
fires. Even when nothing actually happens, long and complicated functions take time and
this may
affect the usability of the site: everything goes very slowly, especially on old computers.
4. Mousing out of a layer:
In a layer-based navigation you may need to know when the mouse leaves a layer so that it
can be
closed. Therefore you register an onmouseout event handler to the layer. However, event
bubbling
causes this event handler to fire when the mouse leaves any element inside the layer, too.
5. Mouseover and mouseout:
Take another look at the example, switch the mouseovers on and try them. The example
adds an
onmouseover event handler to ev3 only. However, you’ll notice that a mouseover event
takes place
not only when the mouse enters ev3's area, but also when it enters the area of ev4 or the
span. In
Mozilla before 1.3, the event even fires when the mouse enters the area of a text!
The reason for this is of course event bubbling. The user causes a mouseover event on ev4.
There is
no onmouseover event handler on this element, but there is one on ev3. As soon as the
event has
bubbled up to this element, the event handler is executed.
6. Mouseenter and mouseleave:
Microsoft has another solution. It has created two new events mouseenter and mouseleave.
They are
almost the same as mouseover and mouseout except that they don’t react to event bubbling.
Therefore they see the entire HTML element they’re registered to as one solid block and
don’t react
to mouseovers and –outs taking place inside the block.
Key Events:
Key Event are used to capture the interaction made by the user by usingkey.
Attribute Value Description
onkeydown script Fires when a user is pressing a key
onkeypress script Fires when a user presses a key
onkeyup script Fires when a user releases a key
<!DOCTYPE HTML>
<html>
<head>
<title>Example: Working with Mouse events</title>
<style>
body{font-family:Verdana; background:#44c767;
color:#fff;}
</style>
<script>
var count = 0; function /* initializes a counter to track the number of mouse movements over
a button.*/
tracker(){ count++; /* Increments the count and displays an alert with the updated count.*/
OUTPUT
JavaScript on Key event handler.
<!DOCTYPE HTML>
<html>
<head>
<title>Example: Working with form Events</title>
<style type="text/css">
p{font-family:Verdana;
background:#FA8B7C; /* Light pink background */
color:#fff; /* White text */
padding:10px; /* Padding inside paragraph */
border:4px solid #555;} /* Dark grey border */
</style>
</head>
<body>
<form>
<p>
<label for="name"> Subject Name:
<input autofocus id="name" name="name" /></label>
/*autofocus, meaning it will be focused when the page loads. */
</p>
<p>
<label for="nick"> Subject Abbrivation:
<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++) // Assign event handler to each input
{
items[i].onkeyup = keyboardEventHandler;
}
function keyboardEventHandler(e){
document.getElementById("output").innerHTML = "Key pressed is: " +
e.keyCode + " Char:" + String.fromCharCode(e.keyCode);
}
</script>
</html>
OUTPUT