日期:2014-05-16  浏览次数:20409 次

Javascript,Jquery笔记
  • $.ajax + Spring MVC
    $.ajax({
    	async : true , 
    	type : 'POST', 
    	url:'someurl', 
    	contentType: "application/json; charset=utf-8", //
    	processData:false, //To avoid making query String instead of JSON
    	data: JSON.stringify({}), //some parameters. Json Object.
    	success: function(data){
    	},
    	error: function(e){
    	}
    });
    ?
    @RequestMapping("/someurl")
    	public @ResponseBody Map<String, Object> someMethod(@RequestBody Map<String, Object> params){
    		//do sth.
    	}
    ?
  • window.location
    location = {  
        host: "localhost:8080",   
        hostname: "localhost",   
        href: "http://localhost:8080/webcontext/index.html",   
        origin: "http://localhost:8080",   
        pathname: "/webcontext/index.html",   
        port: "8080",   
        protocol: "http:",   
        reload: function reload() { [native code] },   
        replace: function () { [native code] },   
        ...  
    };  
    
    location.pathname = 'someUrl'; // 跳转到http://localhost:8080/someUrl
    location.href = 'someUrl'; //跳转到http://localhost:8080/webcontext/someUrl
    location.href = 'http://www.google.com'; //跳转到http://www.google.com
    ?
  • 序列化
    $.fn.serializeObject = function(){
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
            if (o[this.name] !== undefined) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };
    ?
  • outerHtml
    $.fn.outerHtml = function(){
    	return $("<p>").append(this.clone()).html();
    };
    
    ?
  • 表单清空
    $(':input','someFormSelector')  
    	.not(':button, :submit, :reset, :hidden')  
    	.val('')  
    	.removeAttr('checked')  
    	.removeAttr('selected');
    ?
  • 在JS文件中引用外部JS文件
    document.write("<script language='javascript' src='someUrl/someScript.js' charset='utf-8'></script>");
    //注意charset!!
    ?
  • jquery.cookie.js(我写了半天还不如源码清晰明白, orZ)
    /*!
     * jQuery Cookie Plugin v1.4.0
     * https://github.com/carhartl/jquery-cookie
     *
     * Copyright 2013 Klaus Hartl
     * Released under the MIT license
     */
    (function (factory) {
    	if (typeof define === 'function' && define.amd) {
    		// AMD
    		define(['jquery'], factory);
    	} else if (typeof exports === 'object') {
    		// CommonJS
    		factory(require('jquery'));
    	} else {
    		// Browser globals
    		factory(jQuery);
    	}
    }(function ($) {
    
    	var pluses = /\+/g;
    
    	function encode(s) {
    		return config.raw ? s : encodeURIComponent(s);
    	}
    
    	function decode(s) {
    		return config.raw ? s : decodeURIComponent(s);
    	}
    
    	function stringifyCookieValue(value) {
    		return encode(config.json ? JSON.stringify(value) : String(value));
    	}
    
    	function parseCookieValue(s) {
    		if (s.indexOf('"') === 0) {
    			// This is a quoted cookie as according to RFC2068, unescape...
    			s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
    		}
    
    		try {
    			// Replace server-side written pluses with spaces.
    			// If we can't decode the cookie, ignore it, it's unusable.
    			// If we can't parse the cookie, ignore it, it's unusable.
    			s = decodeURIComponent(s.replace(pluses, ' '));
    			return config.json ? JSON.parse(s) : s;
    		} catch(e) {}
    	}
    
    	function read(s, converter) {
    		var value = config.raw ? s : parseCookieValue(s);
    		return $.isFunction(converter) ? converter(value) : value;
    	}
    
    	var config = $.cookie = function (key, value, options) {
    
    		// Write
    
    		if (value !== undefined && !