jQuery实用技巧必备(上)


Posted in Javascript onNovember 02, 2015

本文实例总结了经典且实用的jQuery代码开发技巧。分享给大家供大家参考。具体如下:

1. 禁止右键点击

$(document).ready(function(){
 $(document).bind("contextmenu",function(e){
 return false;
 });
});

2. 隐藏搜索文本框文字
Hide when clicked in the search field, the value.(example can be found below in the comment fields)

$(document).ready(function() {
$("input.text1").val("Enter your search text here");
 textFill($('input.text1'));
});
 
 function textFill(input){ //input focus text function
 var originalvalue = input.val();
 input.focus( function(){
  if( $.trim(input.val()) == originalvalue ){ input.val(''); }
 });
 input.blur( function(){
  if( $.trim(input.val()) == '' ){ input.val(originalvalue); }
 });
}

3. 在新窗口中打开链接
XHTML 1.0 Strict doesn't allow this attribute in the code, so use this to keep the code valid.

$(document).ready(function() {
 //Example 1: Every link will open in a new window
 $('a[href^="http://"]').attr("target", "_blank");
 
 //Example 2: Links with the rel="external" attribute will only open in a new window
 $('a[@rel$='external']').click(function(){
 this.target = "_blank";
 });
});
// how to use
<a href="http://www.3water.com" rel=external>open link</a>

4. 检测浏览器
注: 在版本jQuery 1.4中,$.support 替换掉了$.browser 变量

$(document).ready(function() {
// Target Firefox 2 and above
if ($.browser.mozilla && $.browser.version >= "1.8" ){
 // do something
}

// Target Safari
if( $.browser.safari ){
 // do something
}

// Target Chrome
if( $.browser.chrome){
 // do something
}

// Target Camino
if( $.browser.camino){
 // do something
}

// Target Opera
if( $.browser.opera){
 // do something
}

// Target IE6 and below
if ($.browser.msie && $.browser.version <= 6 ){
 // do something
}

// Target anything above IE6
if ($.browser.msie && $.browser.version > 6){
 // do something
}
});

5. 预加载图片
This piece of code will prevent the loading of all images, which can be useful if you have a site with lots of images.

$(document).ready(function() {
jQuery.preloadImages = function()
{
 for(var i = 0; i<ARGUMENTS.LENGTH; jQuery(?<img { i++)>").attr("src", arguments[i]);
 }
}
// how to use
$.preloadImages("image1.jpg");
});

6. 页面样式切换

$(document).ready(function() {
 $("a.Styleswitcher").click(function() {
 //swicth the LINK REL attribute with the value in A REL attribute
 $('link[rel=stylesheet]').attr('href' , $(this).attr('rel'));
 });
// how to use
// place this in your header
<LINK rel=stylesheet type=text/css href="default.css">
// the links
<A class=Styleswitcher href="#" rel=default.css>Default Theme</A>
<A class=Styleswitcher href="#" rel=red.css>Red Theme</A>
<A class=Styleswitcher href="#" rel=blue.css>Blue Theme</A>
});

7. 列高度相同
如果使用了两个CSS列,使用此种方式可以是两列的高度相同。

$(document).ready(function() {
function equalHeight(group) {
 tallest = 0;
 group.each(function() {
 thisHeight = $(this).height();
 if(thisHeight > tallest) {
  tallest = thisHeight;
 }
 });
 group.height(tallest);
}
// how to use
$(document).ready(function() {
 equalHeight($(".left"));
 equalHeight($(".right"));
});
});

8. 动态控制页面字体大小
用户可以改变页面字体大小

$(document).ready(function() {
 // Reset the font size(back to default)
 var originalFontSize = $('html').css('font-size');
 $(".resetFont").click(function(){
 $('html').css('font-size', originalFontSize);
 });
 // Increase the font size(bigger font0
 $(".increaseFont").click(function(){
 var currentFontSize = $('html').css('font-size');
 var currentFontSizeNum = parseFloat(currentFontSize, 10);
 var newFontSize = currentFontSizeNum*1.2;
 $('html').css('font-size', newFontSize);
 return false;
 });
 // Decrease the font size(smaller font)
 $(".decreaseFont").click(function(){
 var currentFontSize = $('html').css('font-size');
 var currentFontSizeNum = parseFloat(currentFontSize, 10);
 var newFontSize = currentFontSizeNum*0.8;
 $('html').css('font-size', newFontSize);
 return false;
 });
});

9. 返回页面顶部功能
For a smooth(animated) ride back to the top(or any location).

$(document).ready(function() {
$('a[href*=#]').click(function() {
 if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
 && location.hostname == this.hostname) {
 var $target = $(this.hash);
 $target = $target.length && $target
 || $('[name=' + this.hash.slice(1) +']');
 if ($target.length) {
 var targetOffset = $target.offset().top;
 $('html,body')
 .animate({scrollTop: targetOffset}, 900);
 return false;
 }
 }
 });
// how to use
// place this where you want to scroll to
<A name=top></A>
// the link
<A href="#top">go to top</A>
});

10. 获得鼠标指针XY值
Want to know where your mouse cursor is?

$(document).ready(function() {
 $().mousemove(function(e){
 //display the x and y axis values inside the div with the id XY
 $('#XY').html("X Axis : " + e.pageX + " | Y Axis " + e.pageY);
 });
// how to use
<DIV id=XY></DIV>

});

11.返回顶部按钮
你可以利用 animate 和 scrollTop 来实现返回顶部的动画,而不需要使用其他插件。

// Back to top
$('a.top').click(function () {
 $(document.body).animate({scrollTop: 0}, 800);
 return false;
});
<!-- Create an anchor tag -->
<a class="top" href="#">Back to top</a>

改变scrollTop 的值可以调整返回距离顶部的距离,而 animate 的第二个参数是执行返回动作需要的时间(单位:毫秒)。

今天为大家先介绍一部分jQuery技巧,后续文章会继续更新,希望大家持续关注。

Javascript 相关文章推荐
你真的了解JavaScript吗?
Feb 24 Javascript
浅谈jQuery中height与width
Jul 06 Javascript
基于zepto的移动端轻量级日期插件--date_picker
Mar 04 Javascript
javascript中call apply 与 bind方法详解
Mar 10 Javascript
终于实现了!精彩的jquery弹幕效果
Jul 18 Javascript
浅谈js中几种实用的跨域方法原理详解
Dec 02 Javascript
详解jQuery简单的表单应用
Dec 16 Javascript
Web前端框架Angular4.0.0 正式版发布
Mar 28 Javascript
详解js的视频和音频采集
Aug 09 Javascript
小程序实现列表删除功能
Oct 30 Javascript
Vue脚手架编写试卷页面功能
Mar 17 Javascript
vue导入.md文件的步骤(markdown转HTML)
Dec 31 Vue.js
jQuery zclip插件实现跨浏览器复制功能
Nov 02 #Javascript
JQuery zClip插件实现复制页面内容到剪贴板
Nov 02 #Javascript
jquery实现简洁文件上传表单样式
Nov 02 #Javascript
Jquery效果大全之制作电脑健康体检得分特效附源码下载
Nov 02 #Javascript
js实现动态加载脚本的方法实例汇总
Nov 02 #Javascript
Jquery时间轴特效(三种不同类型)
Nov 02 #Javascript
Jquery全屏相册插件zoomvisualizer具有调节放大与缩小功能
Nov 02 #Javascript
You might like
提高php运行速度的一些小技巧分享
2012/07/03 PHP
thinkphp实现发送邮件密码找回功能实例
2014/12/01 PHP
php建立Ftp连接的方法
2015/03/07 PHP
php抽奖概率算法(刮刮卡,大转盘)
2020/04/17 PHP
php反射学习之依赖注入示例
2019/06/14 PHP
firefox中JS读取XML文件
2006/12/21 Javascript
使用prototype.js 的时候应该特别注意的几个问题.
2007/04/12 Javascript
js弹出层之1:JQuery.Boxy (二)
2011/10/06 Javascript
关闭浏览器时提示onbeforeunload事件
2013/12/25 Javascript
jQuery插件jRumble实现网页元素抖动
2015/06/05 Javascript
js仿微信语音播放实现思路
2016/12/12 Javascript
快速实现JS图片懒加载(可视区域加载)示例代码
2017/01/04 Javascript
fullpage.js最后一屏滚动方式
2018/02/06 Javascript
element ui里dialog关闭后清除验证条件方法
2018/02/26 Javascript
vue项目部署上线遇到的问题及解决方法
2018/06/10 Javascript
vue自动化路由的实现代码
2019/09/30 Javascript
node运行js获得输出的三种方式示例详解
2020/07/02 Javascript
JavaScript代码简化技巧实例解析
2020/09/09 Javascript
使用Vant完成通知栏Notify的提示操作
2020/11/11 Javascript
在Python中移动目录结构的方法
2016/01/31 Python
Python单体模式的几种常见实现方法详解
2017/07/28 Python
python爬虫获取多页天涯帖子
2018/02/23 Python
Python for循环中的陷阱详解
2018/07/13 Python
Python文件操作方法详解
2020/02/09 Python
浅析Python __name__ 是什么
2020/07/07 Python
如何卸载python插件
2020/07/08 Python
Javascript 高级手势使用介绍
2013/04/21 HTML / CSS
全球在线商店:BerryLook
2019/04/14 全球购物
Herschel Supply Co.美国:背包、手提袋及配件
2020/11/24 全球购物
暑期实习鉴定
2013/12/16 职场文书
开学典礼策划方案
2014/05/28 职场文书
2014年大学宣传部工作总结
2014/12/19 职场文书
社区六一儿童节活动总结
2015/02/11 职场文书
2015最新学生自我评价范文
2015/03/03 职场文书
JavaScript中document.activeELement焦点元素介绍
2021/11/27 Javascript
Python中的pprint模块
2021/11/27 Python