<html> <head> <title>IP转换</title> <script> function ip2long() { var ipv4=document.getElementById('aa').value; var aIPsec=ipv4.split("."); for(var i=0;i<4;i++) { if(parseInt(aIPsec[i])<16) { aIPsec[i]="0"+parseInt(aIPsec[i]).toString(16); } else { aIPsec[i]=parseInt(aIPsec[i]).toString(16); } } var nIPaddr=parseInt("0x"+aIPsec[0]+aIPsec[1]+aIPsec[2]+aIPsec[3]); document.getElementById('a').innerHTML=nIPaddr; } function long2ip () { var proper_address=document.getElementById('bb').value; var output = false; if ( !isNaN ( proper_address )) { output = ( (proper_address>>24) & 0xff ) + '.' +( (proper_address>>16) & 0xff ) + '.' +( (proper_address>>8) & 0xff ) + '.' +( proper_address & 0xff ); } document.getElementById('b').innerHTML=output; } </script> </head> <body> <input type="text" id="aa" /><input type="button" οnclick="ip2long()" value="IP转换为十进制" /> <div id='a'></div> <input type="text" id="bb" /><input type="button" οnclick="long2ip ()" value="十进制转换为IP" /> <div id='b'></div> </body> </html>