<<上篇 | 首页 | 下篇>>

jQuery与prototype等其他js库共存冲突解决

方法一:

<html>  
 <head> 
   <script src="prototype.js"></script> 
   <script src="jquery.js"></script> 
   <script> 
     jQuery.noConflict();  
       
     // Use jQuery via jQuery(...)  
     jQuery(document).ready(function(){  
       jQuery("div").hide();  
     });  
       
     // Use Prototype with $(...), etc.  
     $('someid').style.display = 'none';  
   </script> 
 </head> 
 <body></body> 
 </html>

方法二:

<html>  
 <head> 
   <script src="prototype.js"></script> 
   <script src="jquery.js"></script> 
   <script> 
     var $j = jQuery.noConflict();  
       
     // Use jQuery via $j(...)  
     $j(document).ready(function(){  
       $j("div").hide();  
     });  
       
     // Use Prototype with $(...), etc.  
     $('someid').style.display = 'none';  
   </script> 
 </head> 
 <body></body> 
 </html> 

方法三:

<html>  
 <head> 
   <script src="prototype.js"></script> 
   <script src="jquery.js"></script> 
   <script> 
     jQuery.noConflict();  
       
     // Put all your code in your document ready area  
     jQuery(document).ready(function($){  
       // Do jQuery stuff using $  
       $("div").hide();  
     });  
       
     // Use Prototype with $(...), etc.  
     $('someid').style.display = 'none';  
   </script> 
 </head> 
 <body></body> 
 </html> 

原文档http://docs.jquery.com/Using_jQuery_with_Other_Libraries
标签 :

jQuery.ajax传递中文参数乱码的解决方法

许多人在使用JQuery.ajax方法时肯定会遇到一个问题。在编码不是UTF-8的时候,当传递的参数里有中文的时候,服务端Request的 时候都会出现乱码。本人最近也遇到了需要传递中文参数的问题。在网上搜索一下,复制粘贴发的到处都是的“终极”“解决方案”无非就是 escape(str)来转码,然后在服务端还要写个方法再编辑一次,或用System.Text.Encoding下的方法来换来换去。

 很久以前一直在使用Prototype框架。在.net-GB2312或jsp-utf8下都使用过,从来没遇到有字符编码的问题。于是将Prototype和JQuery代码都下载下来打开研究原因。具体结果如下

diyblPic


不同之处在于JQuery默认的contentType:application/x-www-form-urlencoded

而Prototype则是contentType:application/x-www-form-urlencoded; charset=UTF-8

这才是JQuery正在乱码的原因,在未指定字符集的时候,是使用ISO-8859-1

ISO8859-1,通常叫做Latin-1。Latin-1包括了书写所有西方欧洲语言不可缺少的附加字符。

JQuery的Ajax根本没有考虑到国际化的问题,使用了欧洲的字符集,所以才引起了传递中文出现乱码的问题。

而我们的UTF-8则可以解决这一问题。

 

最终指需要修改JQuery的代码,显式声明contentType使用utf-8字符集,即可解决GB2312中文传递的问题。

修改如图:

diyblPic

只需要简单的将JQuery的jquery.js代码加以修改,将contentType:application/x-www-form-urlencoded 加上charset=UTF-8就可以了,这样不需要改变改什么web.config或什么在页面中改编码什么的了,也不需要用escapc(str)再在服务端解码。英文怎么传递,中文也怎么传递。


另外,增加Servlet filter,记得要放到web.xml Filter最前端,特别是EncodeFilter前:
Java代码
  1. public void doFilter(ServletRequest servletRequest,  
  2.             ServletResponse servletResponse, FilterChain filterChain)  
  3.             throws IOException, ServletException {  
  4.         if (logger.isDebugEnabled()) {  
  5.             logger  
  6.                     .debug("doFilter(ServletRequest, ServletResponse, FilterChain) - start"); //$NON-NLS-1$  
  7.         }  
  8.   
  9.         HttpServletRequest request = (HttpServletRequest) servletRequest;  
  10.         HttpServletResponse response = (HttpServletResponse) servletResponse;  
  11.   
  12.         String requestedWith = request.getHeader("x-requested-with");  
  13.         String type = request.getContentType();  
  14.         if (requestedWith != null && "XMLHttpRequest".equals(requestedWith)  
  15.                 && null != type  
  16.                 && "application/x-www-form-urlencoded".equals(type)) {  
  17.             logger.info("This's ajax post.set encode is UTF-8.");  
  18.             request.setCharacterEncoding("UTF-8");  
  19.             response.setCharacterEncoding("UTF-8");  
  20.             request.getParameterMap();  
  21.         }  
  22.   
  23.         filterChain.doFilter(request, response);  
  24.   
  25.         if (logger.isDebugEnabled()) {  
  26.             logger  
  27.                     .debug("doFilter(ServletRequest, ServletResponse, FilterChain) - end"); //$NON-NLS-1$  
  28.         }  
  29.     }  


Java代码
  1. <filter>  
  2.     <filter-name>encodingFilter</filter-name>  
  3.     <filter-class>  
  4.         org.springframework.web.filter.CharacterEncodingFilter  
  5.     </filter-class>  
  6.     <init-param>  
  7.         <param-name>encoding</param-name>  
  8.         <param-value>GBK</param-value>  
  9.     </init-param>  
  10.     <init-param>  
  11.         <param-name>forceEncoding</param-name>  
  12.         <param-value>true</param-value>  
  13.     </init-param>  
  14. </filter>  
  15. <filter>  
  16.     <filter-name>ajaxEncodeFilter</filter-name>  
  17.     <filter-class>  
  18.         com.pengpeng.framework.common.filter.AjaxEncodeFilter  
  19.     </filter-class>  
  20. </filter
标签 :

240个jQuery插件

jQuery 是继 prototype 之后又一个优秀的 Javascript 框架。其宗旨是—写更少的代码,做更多的事情。它是轻量级的 js 库(压缩后只有21k) ,这是其它的 js 库所不jquery及 的,它兼容 CSS3,还兼容各种浏览器(IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+)。 jQuery 是一个快速的,简洁的 javaScript 库,使用户能更方便地处理 HTML documents、events、实现动画效果,并且方便地为网站提供 AJAX 交互。 jQuery 还有一个比较大的优势是,它的文档说明很全,而且各种应用也说得很详细,同时还有许多成熟的插件可供选择。 jQuery 能够使用户的 html 页保持代码和 html 内容分离,也就是说,不用再在 html 里面插入一堆js来调用命令了,只需定义 id 即可。今天在Kollermedia.at上发现了一篇JQuery插件列表的文章,特推荐如下。
240个jQuery插件

阅读全文……