代码改变世界

一些JQuery使用技巧

2017-06-16 16:56  taozsay  阅读(274)  评论(0编辑  收藏  举报

最近做项目,在使用JQuery中遇到一些问题记录下。

 

1.根据Id查询父级内容,或者父级的父级

之前会使用$("#id").parent().parent();

这种使用有很大的弊端,现在可使用closest 函数,在需要查询的父级定义一个class,

例如:$("#id").closest(".class");

 

2.设置CheckBox 的勾选/取消勾选问题

一般使用$("[name=items]:checkbox").attr("checked", true); 

但是第一次执行,没问题,但第二次执行就有问题了,选择不了

解决办法:把attr()换成prop()

例如:$("[name=items]:checkbox").prop("checked", true);

参考:http://www.cnblogs.com/KeenLeung/p/3799895.html

 

3.使用trigger触发事件

例如:$("#id").trigger("click");

 

4.判断是否是移动端

if('ontouchstart' in document){

  //todo

}

====2017.7.20 补充======

上面判断是否移动端的有个bug.

更新为下面这个函数

 1 //判断是否是PC
 2 function IsPC(){    
 3     var userAgentInfo = navigator.userAgent;  
 4     var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod","BlackBerry");    
 5     var flag = true;    
 6     for (var v = 0; v < Agents.length; v++) {    
 7         if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }    
 8      }    
 9     return flag;    
10 }

 

 

5.table 固定表头

表头不能换行,内容可以换行。

给table添加样式 table-layout:fixed

 

6.表单提交特殊字符处理,预防XXS漏洞

http://www.cnblogs.com/bigbrid/p/6909716.html

 

7.AJAX跨域提交表单

http://www.cnblogs.com/bigbrid/p/6542648.html

 

8.页面中经常用到弹窗,关闭窗口之类。有个方法获取当前窗口和当前窗口索引

 1 var topformwindow=window;
 2 var currentIndex=0;
 3 var currentPageIndex=0;
 4 
 5 function getFormWindow()
 6 {
 7     var  formWindow=window;
 8     var  lastWindow=formWindow;
 9     try{
10         for(i=0;i<10;i++){
11             if(formWindow.tempCode!=undefined && formWindow.tempCode!="")
12             {
13                lastWindow=formWindow;
14                
15                if(formWindow.parent==null){
16                    currentPageIndex=lastWindow.currentIndex;
17                     return lastWindow;   
18                }
19                formWindow=formWindow.parent;
20             }else{
21                 currentPageIndex=lastWindow.currentIndex;
22                return lastWindow;
23            }
24         }
25         currentPageIndex=lastWindow.currentIndex;
26         return lastWindow;
27     }
28     catch(ex){
29         currentPageIndex=lastWindow.currentIndex;
30         return lastWindow;    
31     }
32 }

 

9.手机端使用的弹层组件

之前在PC端弹层组件用的artDialog ,现在发现没有移动端,

经调研发现layer不错,现在移动端的弹窗组件就用它了。 

 

 10.Table 兼容手机端样式

https://css-tricks.com/examples/ResponsiveTables/responsive.php

 

11.IE9以下版本,Ajax的error提示no transport的问题

1 jQuery.support.cors = true;
2 
3 $.ajax({
4   crossDomain: true,
5   url: "",
6   type: "POST",
7   dataType: "xml",
8   data: soapMessage,
9 });

添加完上面设置发现IE9还是报错,最后发现是IE浏览器的安全性设置问题,

解决方法如下:点击IE浏览器的的“工具->Internet 选项->安全->自定义级别”将“其他”选项中的“通过域访问数据源”选中为“启用”或者“提示”,点击确定就可以了。

 

12.IE下Ajax 请求有缓存

解决方法:

1.在AJAX请求的页面后加个随机函数,我们可以使用随机时间函数
在javascript发送的URL后加上t=Math.random()

例如这样:URL+"&"+"t="+Math.random();或者new Date();
在 URL 参数后加上 "?timestamp=" + new Date().getTime();

2.最好的方法:
$.ajaxSetup({cache:false})

这样页面中所有的ajax请求时,都执行这个。就不必改已经完成的N个接口

 

13.数据的一些基本格式校验

http://www.cnblogs.com/bigbrid/p/7846367.html

 

 

持续更新...