废话不多说,贴代码!
<pre name="code" class="html"><!DOCTYPE html> <html> <head> <title>Get my IP</title> </head> <body> <a href="http://www.whatsmyip.us">whatsmyip</a> My Real IP is: <script type="text/javascript" src="http://www.whatsmyip.us/showipsimple.php"></script> <script type="text/javascript"> document.write("<br/>"); </script> <a href="http://pv.sohu.com/cityjson?ie=utf-8">Sohu API</a> My IP Info: <script type="text/javascript" src="http://pv.sohu.com/cityjson?ie=utf-8"> </script> <script type="text/javascript"> var cip = returnCitySN.cip; var cid = returnCitySN.cid; var cname = returnCitySN.cname; document.write("IP is:" + cip + " " + "Address is:" + cname); </script> <script type="text/javascript"> document.write("<br/>"); </script> <a href="http://whois.pconline.com.cn/ipJson.jsp?callback=myip">PConline[太平洋] API</a> <script type="text/javascript"> /** * [myip description] 这儿用到了回调函数,myip({"ip":"27.17.140.49","pro":"湖北省","proCode":"420000","city":"武汉市","cityCode":"420100","region":"","regionCode":"0","addr":"湖北省武汉市 电信","regionNames":""});,我们可以看到,回调函数myip()里面有参数,仔细观察不难发现是json型数据,我们在回调函数里面用obj去获取它,这个参数随便定都可以,然后哦这个参数就是我们获取的这个json型数据了. * @param {[type]} obj [description] 与返回的json型数据一一对应 * @return {[type]} [description] */ function myip(obj){ document.write("IP is:" + obj.ip + " " + "Address is:" + obj.addr); } </script> <script type="text/javascript" src="http://whois.pconline.com.cn/ipJson.jsp?callback=myip"></script> </body> </html>
相比大家应该一目了然,自己可以把代码copy下去自己是一下就知道了。
时间: 2024-11-06 10:53:26