Tuesday, December 14, 2010

Javascript event, with html beginner simple

Event:

function targetElem(e) {
 var targ;
 if (!e) var e = window.event;
 if (e.target) targ = e.target;
 else if (e.srcElement) targ = e.srcElement;
 if (targ.nodeType == 3) // defeat Safari bug
  targ = targ.parentNode;
}


function keyEvent(e) {
 var code;
 if (!e) var e = window.event;
 if (e.keyCode) code = e.keyCode;
 else if (e.which) code = e.which;
 var character = String.fromCharCode(code);
 alert('Character was ' + character);
}



function rightButtonClick(e) {
 var rightclick;
 if (!e) var e = window.event;
 if (e.which) rightclick = (e.which == 3);
 else if (e.button) rightclick = (e.button == 2);
 alert('Rightclick: ' + rightclick); // true or false
}

function coordinates(e) {
 var posx = 0;
 var posy = 0;
 if (!e) var e = window.event;
 if (e.pageX || e.pageY)  {
  posx = e.pageX;
  posy = e.pageY;
 }
 else if (e.clientX || e.clientY)  {
  posx = e.clientX + document.body.scrollLeft
   + document.documentElement.scrollLeft;
  posy = e.clientY + document.body.scrollTop
   + document.documentElement.scrollTop;
 }
 // posx and posy contain the mouse position relative to the document
 // Do something with this information
}

Jishee:

<html>
<body>

<div id="simple"><img src="1"><img src="2"></div>
<script>
var div = document.getElementById('simple');
var imgs = div.getElementsByTagName('img');
if (imgs.length > 0) {
    alert(imgs[0].src);
}
</script>

<div id="message"></div>
<div onMouseOver="over(this);" onMouseOut="out(this);" style="height:20px;background:yellow">mouse over here</div>
<script>
over=function(obj){
document.getElementById('message').innerHTML="--over";
}
out=function(obj){
document.getElementById('message').innerHTML="--out";
}

//var t = document.getElementsByTagName('table')[0];
//var tr = t.getElementsByTagName('tr');
//for(var i = 0; i < tr[0].childNodes.length; i++){
//    alert(tr[0].childNodes[i].innerHTML);
//}
</script>

</body>
</html>

No comments: