js ip地址转真实地址

新浪,有道,ip138提供了将ip地址转换为真实地址的接口,这个控件的作用就是将这些接口封装起来,实现js将ip地址转换为真实地址。

使用方法很简单,如有html

<div class="query_ip">202.103.22.1</div>

调用如下js

$(".query_ip").getaddress({
    type:"youdao" // 支持youdao|sina|ip138 (有道速度快且准确,新浪快,ip138较慢)
});

则将HTML修改为

<div class="query_ip">202.103.22.1 湖北省十堰市 东风电信</div>

具体JS源码如下

(function(){
    /*
     * 默认配置
     */
    var domainAddressConf = {
        source : {
            "sina":"http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=",
            "youdao":"http://youliao.sinaapp.com/app/getip.php?callback=?&type=youdao&ip=",
            "ip138":"http://youliao.sinaapp.com/app/getip.php?callback=?&ip="
        },
        type : null,
        charset : "gb2312"
    }
 
    function getAddress( self,conf ){
        var type = conf.type || "youdao",
            ipadress = conf.source[type] + self.text(),
            api = new getAddressApi( type,ipadress );
 
        api.ready(function( data ){
            self.append( data );
        })
 
        return self;
    }
 
    function getAddressApi( type,conf ){
        var root = this;
 
        this.changeData = function( data ){
            this.data = data;
            return this;
        }
 
        return new getAddressApi[type]( root,conf );
    }
 
    /*
     * 获取新浪ip数据
     */
    getAddressApi.sina = function( root,dataUri ){
 
        this.root = root;
 
        $.getScript(dataUri,function(){
            root.changeData(remote_ip_info);
        });
    }
    getAddressApi.sina.prototype = {
        ready: function( fn ){
            var self = this,
                data = self.root.data;
 
            setTimeout(function(){
                if( data ){
                    fn.call(document,"&nbsp;"+data.province+"省"+data.city+"市&nbsp;"+data.desc);
                }else{
                    self.ready(fn);
                }
            },13);
        }
    }
 
    /*
     * 获取搜狐ip数据
     */
    getAddressApi.youdao = function( root,dataUri ){
 
        this.root = root;
 
        $.getJSON( dataUri, function( data ){
            root.changeData(data);
        });
    }
    getAddressApi.youdao.prototype = {
        ready: function( fn ){
            var self = this,
                data = self.root.data;
 
            setTimeout(function(){
                if( data ){
                    fn.call(document,"&nbsp;"+data[0]);
                }else{
                    self.ready(fn);
                }
            },13);
        }
    }
 
    /*
     * 获取ip138数据
     */
    getAddressApi.ip138 = function( root,dataUri ){
        this.root = root;
 
        $.getJSON( dataUri, function( data ){
            root.changeData(data[0].replace("查询结果:",""));
        });
    }
    getAddressApi.ip138.prototype = {
        ready: function( fn ){
            var self = this,
                data = self.root.data;
 
            setTimeout(function(){
                if( data ){
                    fn.call(document,"&nbsp;"+data);
                }else{
                    self.ready(fn);
                }
            },13);
        }
    }
 
    $.fn.getaddress = function(conf) {
 
        // 判断是否已实例化
        if (this.data("domainaddress")) { return this; }
 
        // 配置
        conf = $.extend(true, {}, domainAddressConf, conf);
 
        this.each(function() {
            var el = new getAddress( $(this), conf );
            $(this).data("domainaddress", el);
        });
 
        return conf.api ? el : this;
    }; 
 
})(jQuery);

因为有道和ip138使用的是xml,为解决跨域,增加了一个php做中转,源码如下

	@header("Content-Type: text/html; charset=utf-8");
	$type = $_GET['type'];
	if( $type == "youdao" ){
		$xml_file_content = fopen_url("http://www.youdao.com/smartresult-xml/search.s?type=ip&q=".$_GET['ip']);
 
		$xml = simplexml_load_string( $xml_file_content );
 
		echo $_GET['callback'].'('.json_encode($xml->product->location).')';
 
	}else{
		$xml_file_content = fopen_url("http://wap.ip138.com/ip.asp?ip=".$_GET['ip']);
 
		preg_match_all( "/\<p \>((.|\n)*)\< \/p\>/s",$xml_file_content,$content );
 
		foreach( $content[1] as $result )
		{
			preg_match_all( "/\<b \>((.|\n)*)\< \/b\>/",$result, $location ); 
 
			echo $_GET['callback'].'('.json_encode($location[1]).')';
		}
 
	}
 
	//Curl 获取网址内容
	function fopen_url($url) 
	{ 
		if (function_exists('file_get_contents')) { 
			$file_content = @file_get_contents($url); 
		} elseif (ini_get('allow_url_fopen') && ($file = @fopen($url, 'rb'))){ 
			$i = 0; 
			while (!feof($file) && $i++ < 1000) { 
				$file_content .= strtolower(fread($file, 4096)); 
			} 
			fclose($file); 
		} elseif (function_exists('curl_init')) { 
			$curl_handle = curl_init(); 
			curl_setopt($curl_handle, CURLOPT_URL, $url); 
			curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,2); 
			curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1); 
			curl_setopt($curl_handle, CURLOPT_FAILONERROR,1); 
			curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Trackback Spam Check'); 
			$file_content = curl_exec($curl_handle); 
			curl_close($curl_handle); 
		} else { 
			$file_content = ''; 
		} 
		return $file_content; 
	}

DEMO:http://www.zhangjingwei.com/getIp_0204_addip138_last.zip

原文链接(607 views)|评论 (2)

无限级菜单

随手写了一个,实现不复杂,只支持动态树。

如果要用的话,获取数据哪里做下简单的修改就可以了,这只是一个Demo。

  (function($){
 
	var jtree = {
		treeid : "jquery_tree_resource",
		treeclass : "tree",
		idname : "id",
		classname : "name",
		optionname : "options",
		openclass: "switch_roots_open",
		closeclass: "switch_roots_close",
		treeurl: ""
	}
 
	// constructor
	function tree(root, conf) {
		var self = root,
		cache = "",    // HTML缓存区
		current = null,    // 当前节点对象
		counter = 0;    // 层级计数器
 
		$.extend(self,{
			/*
			 * 初始化Html
			 * 设置固定的格式以及Id、Class
			 */
			init: function(){
				var el = root.append("< \ul>");
				el.attr({
					"id": conf.treeid,
					"class": conf.treeclass
				});
 
				if( !current ){
					current = root.find("ul");
				}
 
				return self;
			},
 
			getTreeHtml: function(){
				var html = "";
 
				/*
				 * 构建菜单
				 */
				$.each(window.now,function( n,o ){
					html += '<li class="tree-node" id="tree_resource_'+o.id+'"><a id="tree_resource_'+o.id+'_a"><span id="tree_resource_'+o.id+'_span">'+o.name+'</span></a><ul id="tree_resource_'+o.id+'_ul" style="display: none;"></ul></li>';
				});
				cache = html;
 
				return self;
 
			},
 
			/*
			 * 插入HTML代码
			 */
			innerTree: function(){
				current.html( cache ).show("fast");
 
				return self;
			},
 
			/*
			 * 显示菜单
			 */
			showTree: function(){
				current.show("fast");
 
				return self;
			},
 
			/*
			 * 关闭菜单
			 */
			hideTree: function(){
				current.hide("normal");
 
				return self;
			},
 
			/*
			 * 绑定点击事件
			 */
			bindTreeEvent: function(){
				var treeNodes = current.find("a");
 
				/*
				 * 实现菜单
				 */
				$.each(treeNodes,function(n,i){
					$(i).bind("click",function(){
						var s = $(this),
						type = s.data("menuType")
						current = s.nextAll("ul");
						if( !type ){
							if( type === undefined ){
								// ---模拟
								window.now = json_02;
								// 加入新节点
								counter++;
								self.getTreeHtml().innerTree().bindTreeEvent();
							}else{
								self.showTree();
							}
							s.data("menuType",true);
						}else{
							s.data("menuType",false);
							self.hideTree();
						}
					});
				})
				return self;
			}
		});
 
		return self;
	}
 
	$.fn.jtree = function(conf) { 
 
		var el = this.data("jtree");
		if (el) {
			return el;
		}
 
		conf = $.extend({}, jtree, conf); 
 
		this.each(function() {
			el = new tree($(this), conf);
			el.init().getTreeHtml().innerTree().bindTreeEvent();
			$(this).data("jtree", el);
		});
 
		return conf.api ? el: this; 
 
	};
 
  })(jQuery);

数据

  var json_01 = [{"id":"1","name":"111"},{"id":"2","name":"222"}];
 
  var json_02 = [{"id":"3","name":"aaaa"},{"id":"4","name":"bbb"}];

原文链接(60 views)|暂无评论(赶紧抢沙发)

运算符的特殊行为

加法运算符:

  • 运算数是NaN、undefined,则结果为NaN
  • Infinity加Infinity,结果为Infinity
  • -Infinity加-Infinity,结果为-Infinity
  • Infinity加-Infinity,结果为NaN

减法运算符:

  • 运算数是NaN、undefined时,结果为NaN
  • Infinity减Infinity,结果为NaN
  • -Infinity减-Infinity,结果为NaN
  • Infinity减-Infinity,结果为Infinity
  • -Infinity减Infinity,结果为-Infinity

乘法运算符:

  • 运算数是NaN,结果为NaN
  • Infinity乘以0,结果为NaN

除法运算符:

  • 运算数是NaN,结果为NaN
  • Infinity除Infinity,结果为NaN
  • 任何数除Infinity,结果为Infinity
  • 0除非无穷大的数(Infinity),结果为是NaN

取模运算符:

  • 运算数是NaN,结果为NaN
  • 除数是0或者被除数是Infinity,结果为NaN
  • Infinity除Infinity,结果为NaN
  • 除数是Infinity,结果为被除数
  • 被除数是0,结果为0

原文链接(34 views)|沙发已被占领(1)