[转]整理jquery开发技巧
1.创建一个嵌套的过滤器
1.$(jquery).filter(":not(:has(.selected))") //去掉所有不包含class为.selected的元素2.使用has()来判断一个元素是否包含特定的class或者元素
1.$("input").has(".email").addClass("email_icon");//如果class是email的,就添加email_iocn的class3.使用jQuery切换样式
1.$('link[media='screen']').attr('href', 'Alternative.css'); 4.限制选择的区域
1.var in_stock = $('#myid input.myclass');//找到id=myid元素里class=myclass的输入框5.正确使用ToggleClass
不使用情况:
1.a.hasClass('blueButton') ? a.removeClass('blueButton') : a.addClass('blueButton'); 使用ToggleClass
1.a.toggleClass('blueButton');6.浏览器判断
1.if($.browser.msie && $.browser.version > 6) {
//如果是IE6后的版本...
}7.查找一个元素的索引
1.$("ul > li").click(function () {
2. var index =
$(this).prevAll().length;
3.});8.使用jQuery预加载图片
1.$.preloadImages('image1.gif', '/path/to/image2.png', 'some/image3.jpg'); 9.自动的滚动到页面特定区域
1.$('.area_name').autoscroll();10. 关闭右键的菜单
1.$(document).bind('contextmenu',function(e){
return false; });11.判断一个元素是否存在
1.if($('#someDiv').length) {//存在}12.判断一个元素是否为空
1.if($('#keks').html()) { //为空} 13.判断鼠标的左右键点击
1.$("#someelement").live('click', function(e) { 2. if( (!$.browser.msie
&& e.button == 0) || ($.browser.msie && e.button == 1) ) {
3. alert("左键点击"); 4. } 5. else if(e.button == 2) 6. alert("右键点击"); 7.});14. 显示或者删除输入框的缺省值
01.$(".swap").each(function(i){ 02. $(this).focusin(function(){ 03. if ($(this).val() == "请输入") { 04. $(this).val("");
05. } 06. }).focusout(function(){ 07. if ($.trim($(this).val()) == "") {
08. $(this).val("请输入");
09. } 10. }); 11.});15.指定时间后自动隐藏或者关闭元素
1.$(".mydiv").delay(5000).hide('blind', {},
500);16.动态创建元素到DOM
1.varnewgbin1Div = $(''); 2.newgbin1Div.attr('id','gbin1.com').appendTo('body');17.使用jQuery克隆元素
1.varcloned = $('#gbin1div').clone();18.元素屏幕居中
1.jQuery.fn.center = function () { 2. this.css('position','absolute'); 3. this.css('top', ( $(window).height() - this.height() ) / +$(window).scrollTop()
+ 'px');
4. this.css('left', ( $(window).width() - this.width() ) / 2+$(window).scrollLeft()
+ 'px');return this;
5.}
6.$('#gbin1div').center();19.剔除元素中的HTML
01.(function($) { 02. $.fn.stripHtml = function() { 03. var regexp =
/<("[^"]*"|'[^']*'|[^'">])*>/gi;
04. this.each(function() { 05. $(this).html( 06. $(this).html().replace(regexp,"") 07. ); 08. }); 09. return $(this); 10. } 11.})(jQuery);
12.$('p').stripHtml();20.
使用closest来得到父元素
1.$('#searchBox').closest('div');21.使用firebug来记录jQuery事件
1.jQuery.log =
jQuery.fn.log = function (msg) { 2. if (console){
3. console.log("%s:
%o", msg, this); 4. } 5. return this; 6.};
7.$('#someDiv').hide().log('div hidden').addClass('someClass');22.点击链接强制弹出新窗口
1.Query('a.popup').live('click',
function(){
2. newwindow=window.open($(this).attr('href'),'','height=200,width=150');
3. if (window.focus)
{newwindow.focus()} 4. return false; 5.});23.点击链接强制打开新标签页
1.jQuery('a.newTab').live('click',
function(){
2. newwindow=window.open($(this).href); 3. jQuery(this).target = "_blank"; 4. return false; 5.});24.取得鼠标的X和Y坐标
1.$(document).mousemove(function(e){ 2.$(document).ready(function() { 3.$().mousemove(function(e){ 4.$('#XY').html("Gbin1 X Axis : " + e.pageX + " | Gbin1 Y Axis " + e.pageY);
5.});
6.});25.解析XML
1.function parseXml(xml) {
2. //find every Tutorial and print the author
3. $(xml).find("Tutorial").each(function() 4. { 5. $("#output").append($(this).attr("author")
+ "");
6. }); 7.}26.判断一个图片是否加载完全
1.$('#theGBin1Image').attr('src', 'image.jpg').load(function() { 2.alert('This Image Has Been
Loaded'); 3.});[转]整理jquery开发技巧的更多相关文章
- 15个值得开发人员关注的jQuery开发技巧和心得
在这篇文章中,我们将介绍15个让你的jQuery更加有效的技巧,大部分关于性能提升的,希望大家能够喜欢! 1. 尽量使用最新版本的jQuery类库 jQuery项目中使用了大量的创新.最好的方法来提高 ...
- jQuery开发技巧
jQuery 事件 - submit() 方法 $("form").submit(function(e){}); 当提交表单时,会发生 submit 事件. 该事件只适用于表单元素 ...
- 经典收藏 50个jQuery Mobile开发技巧集萃
http://www.cnblogs.com/chu888chu888/archive/2011/11/10/2244181.html 1.Backbone移动实例 这是在Safari中运行的一款Ba ...
- (转)经典收藏 50个jQuery Mobile开发技巧集萃
(原)http://www.cnblogs.com/chu888chu888/archive/2011/11/10/2244181.html 经典收藏 50个jQuery Mobile开发技巧集萃 ...
- jQuery常用技巧-使用的总结
1.关于页面元素的引用 通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用do ...
- jQuery常用技巧
1.关于页面元素的引用 通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用 ...
- 人人必知的10个 jQuery 小技巧
原文地址:http://info.9iphp.com/10-jquery-tips-everyone-should-know/ 人人必知的10个 jQuery 小技巧 收集的10个 jQuery ...
- SQL开发技巧(二)
本系列文章旨在收集在开发过程中遇到的一些常用的SQL语句,然后整理归档,本系列文章基于SQLServer系列,且版本为SQLServer2005及以上-- 文章系列目录 SQL开发技巧(一) SQL开 ...
- 10个jQuery小技巧
收集的10个 jQuery 小技巧/代码片段,可以帮你快速开发. 1.返回顶部按钮 你可以利用 animate 和 scrollTop 来实现返回顶部的动画,而不需要使用其他插件. $('a.top' ...
随机推荐
- WebTours服务的启动
简介: HP loadrunner自带的一个飞机系统订票网站. 启动服务的步骤: 1.启动StartServer.bat 所在的路径: (\HP LoadRunner 12.02 Community ...
- android下拉刷新控件 android-pulltorefresh
运行效果: 介绍:ListView.ViewPager.WevView.ExpandableListView.GridView.(Horizontal)ScrollView.Fragment上下左右拉 ...
- 免费SSL证书申请
https://buy.wosign.com/free/#apply 和 http://www.startssl.com/
- Another Array of Orz Pandas
Another Array of Orz Pandas 题目链接:http://acm.xidian.edu.cn/problem.php?id=1187 线段树 线段树维护区间和以及区间内各个数平方 ...
- html基础及心得
html开始 <adress></adress>斜体(地址) <em><em>斜体(表示强调) <code></code>插入一 ...
- google谷歌翻译插件-网页一键翻译
上个月转载的一篇博文,是推荐的四款非常实用的翻译插件,这几天看这个chrome插件网首页有新增了一个google谷歌翻译插件.我能说实话,这款插件比之前推荐的4款翻译插件更好用吗?也不能完全说是更好用 ...
- Scala AOP
trait Action { def doAction } trait TBeforeAfter extends Action { abstract override def doAction { p ...
- C#模板打印功能-模板为WPS或Excel
//---WPS----- using EtApp = ET; using System.Reflection; using System.Runtime.InteropServices; using ...
- 我的学习记录JAVA第二天
- SweetTips: 快意灵动的Android提示库!
此文章是我在简书的文章,自行搬到博客园.简书地址:SweetTips: 快意灵动的Android提示库! 源码及所在DEMO已上传至GitHub:SweetTips,欢迎大家提Bug,喜欢的话记得St ...