jQuery的小知识的整理
来源:互联网 发布:mac桌面日历备忘录 编辑:程序博客网 时间:2024/05/10 07:15
1、
event.preventDefault();
event.stopPropagation();
event.preventDefault()
该方法将通知 Web 浏览器不要执行与事件关联的默认动作(如果存在这样的动作)。例如,如果 type 属性是 "submit",在事件传播的任意阶段可以调用任意的事件句柄,通过调用该方法,可以阻止提交表单。注意,如果 Event 对象的 cancelable 属性是 fasle,那么就没有默认动作,或者不能阻止默认动作。无论哪种情况,调用该方法都没有作用。
event.stopPropagation()
该方法将停止事件的传播,阻止它被分派到其他 Document 节点。在事件传播的任何阶段都可以调用它。注意,虽然该方法不能阻止同一个 Document 节点上的其他事件句柄被调用,但是它可以阻止把事件分派到其他节点。
经常用在例如,一个click触发多个事件的时候
阻断掉其他事件的相应
2、
0 == '0'
===是强等于符号
3、
jQuery的bind函数,传参数问题:
$('#startDateID').bind('click', starD.showStartDate);
showStartDate: function (event) {
}
很多时候,我们需要向showStartDate中传入参数
这个时候,我们有两种做法
方法a:
$('#startDateID').bind('click', function(event) {
starD.showStartDate(你要的参数);
});
方法b:
$('#startDateID').bind('click', {arg1: 'test'},starD.showStartDate);
showStartDate: function (event) {
event.data.arg1使用!
}
4、string对象转化为时间对象
var s = '2014-2-12' //这个ie和ff不支持
var t = new Date(s)
s = '2014/2/12' //ie ff chrome都支持
或者new Date('2014','2','12') 也是对各个浏览器兼容的
5、判断浏览器的版本:
保存 这篇解释以备不时之需:
<!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]-->
<!--[if IE]> 所有的IE可识别 <![endif]-->
<!--[if IE 6]> 仅IE6可识别 <![endif]-->
<!--[if lt IE 6]> IE6以及IE6以下版本可识别 <![endif]-->
<!--[if gte IE 6]> IE6以及IE6以上版本可识别 <![endif]-->
<!--[if IE 7]> 仅IE7可识别 <![endif]-->
<!--[if lt IE 7]> IE7以及IE7以下版本可识别 <![endif]-->
<!--[if gte IE 7]> IE7以及IE7以上版本可识别 <![endif]-->
<!--[if IE 8]> 仅IE8可识别 <![endif]-->
<!--[if IE 9]> 仅IE9可识别 <![endif]-->
NOT运算符。这是摆立即在前面的功能,操作员,或子表达式扭转布尔表达式的意义。lt[if lt IE 5.5]The less-than operator. Returns true if the first argument is less than the second argument.
小于运算符。如果第一个参数小于第二个参数,则返回true。lte[if lte IE 6]The less-than or equal operator. Returns true if the first argument is less than or equal to the second argument.
小于或等于运算。如果第一个参数是小于或等于第二个参数,则返回true。gt[if gt IE 5]The greater-than operator. Returns true if the first argument is greater than the second argument.
大于运算符。如果第一个参数大于第二个参数,则返回true。gte[if gte IE 7]The greater-than or equal operator. Returns true if the first argument is greater than or equal to the second argument.
大于或等于运算。如果第一个参数是大于或等于第二个参数,则返回true。( )[if !(IE 7)]Subexpression operators. Used in conjunction with boolean operators to create more complex expressions.
子表达式运营商。在与布尔运算符用于创建更复杂的表达式。&[if (gt IE 5)&(lt IE 7)]The AND operator. Returns true if all subexpressions evaluate to true
AND运算符。如果所有的子表达式计算结果为true,返回true|[if (IE 6)|(IE 7)]The OR operator. Returns true if any of the subexpressions evaluates to true.
OR运算符。返回true,如果子表达式计算结果为true。
7、jQuery不支持json跟string的转换
可以使用json2.js的js库,排除在没必要使用的时候,就不引入
if(typeof(JSON) == "undefined"){
$.getScript('json2.js', function(){
t = JSON.stringify(s);
});
} else{
t = JSON.stringify(s);
}
8、逗号运算符:
在很多场景中,我们会用到这样的方法:
return a,b;
这个表达式,是无论如何都返回b的。
但是a是被执行的!
也就是说,retuan a(),b;
很多时候,我们虽然需要返回的b值,这样同时a函数也被执行了。
是一种比较装B的写法- 。-
9、
jQuery判断ie浏览器的类型:
在jQuery版本1.9.1之后,已经移除了$.browser 和 $.browser.version
取而代之的是 $.support 。 在更新的 2.0 版本中,将不再支持 IE 6/7/8。 以后,如果用户需要支持 IE 6/7/8,只能使用 jQuery 1.9。 如果要全面支持 IE,并混合使用 jQuery 1.9 和 2.0, 官方的解决方案是:
<!--[if lt IE 9]> <script src='jquery-1.9.0.js'></script><![endif]--><!--[if gte IE 9]> <script src='jquery-2.0.0.js'></script><![endif]-->
从长久来看,这样有利于在复杂情况下根据浏览器特性进行分别处理, 而不是简单的检测浏览器类型和版本。 但目前很多旧程序的移植恐怕无法直接过渡为根据浏览器支持特性, 所以在网上找了一些能够直接替换的解决办法。
这个是手动去解析浏览器类型的方法:
$.browser.mozilla = /firefox/.test(navigator.userAgent.toLowerCase());$.browser.webkit = /webkit/.test(navigator.userAgent.toLowerCase());$.browser.opera = /opera/.test(navigator.userAgent.toLowerCase());$.browser.msie = /msie/.test(navigator.userAgent.toLowerCase());
检查是否为 IE 6-8:
if (!$.support.leadingWhitespace) {}
10、
使用jQuery手动封装mask和unmask浮层方法:
//$.extend($.fn, {// mask: function (msg, maskDivClass) {// this.unmask();// var op = {// opacity: 0.8,// z: 10000,// bgcolor: '#ccc'// };// var original = $(document.body);// var position = { top: 0, left: 0 };// if (this[0] && this[0] !== window.document) {// original = this;// position = original.position();// }// var maskDiv = $('<div class="maskdivgen"> </div>');// maskDiv.appendTo(original);// var maskWidth = original.outerWidth();// if (!maskWidth) {// maskWidth = original.width();// }// var maskHeight = original.outerHeight();// if (!maskHeight) {// maskHeight = original.height();// }// maskDiv.css({// position: 'absolute',// top: position.top,// left: position.left,// 'z-index': op.z,// width: maskWidth,// height: maskHeight,// 'background-color': op.bgcolor,// opacity: 0// });// if (msg) {// var msgDiv = $('<div style="position:absolute;border:#6593cf 1px solid;padding:2px;background:#ccca"><div style="line-height:24px;border:#a3bad9 1px solid;background:white;padding:2px 10px 2px 10px">' + msg + '</div></div>');// msgDiv.appendTo(maskDiv);// var widthspace = (maskDiv.width() - msgDiv.width());// var heightspace = (maskDiv.height() - msgDiv.height());// msgDiv.css({// cursor: 'wait',// top: (heightspace / 2 - 2),// left: (widthspace / 2 - 2)// });// }// maskDiv.fadeIn('fast', function () {// $(this).fadeTo('slow', op.opacity);// })// return maskDiv;// },// unmask: function () {// var original = $(document.body);// if (this[0] && this[0] !== window.document) {// original = $(this[0]);// }// original.find("> div.maskdivgen").fadeOut('slow', 0, function () {// $(this).remove();// });// }//});//mash和unmask$.extend($.fn, { mask: function (content) { var a = this[0]; if (!a) return console.log("mask", "the cDom object is empty"), this; this.unmask(); var b = {}; b.cssText = a.style.cssText; b.nextSibling = a.nextSibling; b.parentNode = a.parentNode; a.style.position = "absolute"; a.style.display = "block"; // var _ina = document.createElement("container"); // _ina.style.cssText = "position:absolute;top:0;left:0;width:0;height:0;z-index:100;"; // var _inb = document.body; // _inb || document.write('<span id="__body__" style="display:none;">cQuery</span>'); // ((_inb = document.body.firstChild) ? document.body.insertBefore(_ina, _inb) : document.body.appendChild(_ina)); // (_inb = document.getElementById("__body__")) && _inb.parentNode.removeChild(_inb); // var _container = $(_ina); // _container.append(a); a.style.left = (document.documentElement.scrollLeft || document.body.scrollLeft || 0) + Math.max(0, (document.documentElement.clientWidth - a.offsetWidth) / 2) + "px"; a.style.top = (document.documentElement.scrollTop || document.body.scrollTop || 0) + Math.max(0, (document.documentElement.clientHeight - a.offsetHeight) / 2) + "px"; var c = "background:#000;position:absolute;left:0;top:0;width:" + Math.max(document.documentElement.clientWidth, document.documentElement.scrollWidth, document.body.clientWidth, document.body.scrollWidth) + "px;height:" + Math.max(document.documentElement.clientHeight, document.documentElement.scrollHeight, document.body.clientHeight, document.body.scrollHeight) + "px;"; b.maskDiv = document.createElement("div"); b.maskDiv.style.cssText = c + "filter:progid:DXImageTransform.Microsoft.Alpha(opacity=50);opacity:0.5;"; $(b.maskDiv).insertBefore(a); var isIE = /msie/.test(navigator.userAgent.toLowerCase()); isIE && (b.maskIframe = document.createElement("iframe"), b.maskIframe.style.cssText = c + "filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);opacity:0;", $(b.maskIframe).insertBefore(b.maskDiv)); this.data("__mask__", b); return this; }, unmask: function () { if (!this[0]) return console.log("mask", "the cDom object is empty"), this; var a = this.data("__mask__"); a && (this[0].style.cssText = a.cssText, (a.nextSibling ? this.first().insertBefore(a.nextSibling) : this.first().appendTo(a.parentNode)), $(a.maskDiv).remove(), a.maskIframe && $(a.maskIframe).remove(), this.removeData("__mask__")); }});
- jQuery的小知识的整理
- JQuery的一些小知识
- JS事件知识的一些小整理
- jquery中常用的一些知识整理
- 关于jquery,mysql的一些小知识
- 整理QQ空间,摘出来的小知识
- 自己整理的一些小知识,比较杂,适用于菜鸟
- 一个小目标-整理一生的知识,不断凝练升华
- 小知识~关于声明和定义的一点整理
- 整理的知识结构图
- lucene知识的整理
- Ajax的知识整理
- 整理的知识
- 服务端知识的整理
- 整理下最近写java一个小界面程序的知识整理
- C 小知识整理
- listview小知识整理
- listview小知识整理
- 自闭与反自闭
- 关于“数组指针”的一点想法
- C语言 推箱子小游戏(多年前入门时所写)
- 使用gnome-dictionary和dictd
- nginx 编译错误解决方法 [备忘]
- jQuery的小知识的整理
- java中重载与重写的区别
- 别人的傅氏级数展开式函数
- 一些创建特殊矩阵的内置函数的堆积
- UIWebView背景透明且无拖拽后的上下阴影
- javac hadoop2.2.0
- HDU2072 单词数
- HDU2087 剪花布条
- CentOS 6 NTFS移动硬盘 使用实战