javascript的用法

1.检验身份证号码
<SCRIPT language="JavaScript"> 
<!-- 
  function checkIdNO(idStr){
   //validate idStr if it contains 18 number character
   if(! idStr.match(new RegExp("[0-9]{18}"))){
    return false;
   }
   var length = idStr.length;
   //W[i] = 2^(i-1) mod 11
   var wi = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2];
   var ai = ['1','0','X','9','8','7','6','5','4','3','2'];
   var idArr = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
   var sum = 0;
   var mod = 0;
   for(i=0;i<length-1;i++){
    idArr[i]=parseInt(idStr.charAt(i));
    sum += idArr[i] * wi[i];
   }
   mod = sum % 11 ;
   if(ai[mod] == idStr.charAt(length-1)){
    return true;
   }else{
    return false;
   }
  }
//--> 
</script>


2.显示按键以及ACSII码
<script>
function keydown()
{
var keycode=event.keyCode
var Realkey=String.fromCharCode(event.keyCode)
showkeycode.value='按键:    '+Realkey+'\nASCII码: '+keycode
document.form1.realkey.value='按键:    '+Realkey
document.form1.code.value='ASCII码: '+keycode
}
document.onkeypress=keydown //onkeypress事件对应小写字母等,不包含Ctrl、Shift、Caps、Tab
document.onkeydown=keydown //onkeydown事件对应大写字母等,包含……
</script>


3.IE中拷贝代码
<script language=JavaScript>
	<!--
   function copyUrl(url){
   	var content='';
   	window.clipboardData.setData("Text",url);
    alert("复制成功,请粘贴到你的QQ/MSN上推荐给你的好友");
   }
  //-->
</script>


4.执行代码和保存代码
<SCRIPT>
function runCode(obj)
{
var code=obj.value;
var newwin=window.open('','','');
newwin.opener = null;
newwin.document.write(code);
newwin.document.close();
}
function saveCode(obj) {
var newwin = window.open('', '_blank', 'top=10000');
newwin.document.open('text/html', 'replace');
newwin.document.write(obj.value);
newwin.document.execCommand('saveas','','code.htm');
newwin.close();
}
</script>


5.禁止选择、拷贝代码
<TEXTAREA name=run0 >
oncontextmenu="return false" 
ondragstart="return false" 
onselectstart ="return false" 
onselect="document.selection.empty()" 
oncopy="document.selection.empty()" 
onbeforecopy="return false" 
onmouseup="document.selection.empty()"
</TEXTAREA>


6.可拖动的图片
<HTML>
 <HEAD>
  <script language="javascript">
     function moveImage(){
       if(window.event.button!=1){
         return;
       }
       with(window.event.srcElement.style){
         //重新设置图像在浏览器中的位置
         pixelLeft=window.event.x-236/2-document.all.ImageDiv.offsetLeft;
         pixelTop=window.event.y-118/2-document.all.ImageDiv.offsetTop;
       }
       window.event.returnValue=false;//取消系统拖动事件
     }
  </script>
 </HEAD>

 <BODY>
  <div style="position:relative" id="ImageDiv">
     <img id="image2" style="container:positioned;position:absolute;top:60px;left:250px;width:236px;height:108px;" src="Hello2.jpg">
     <img id="image2" style="container:positioned;position:absolute;top:60px;left:60px;width:236px;height:108px;" src="Hello3.jpg" onmouseMove="moveImage()">
  </div>
 </BODY>
</HTML>

你可能感兴趣的:(JavaScript,html,qq,浏览器,IE)