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 !!的作用
Dec 04 Javascript
js String对象中常用方法小结(字符串操作)
Jan 27 Javascript
javascript的渐进增强与平稳退化浅谈
Nov 12 Javascript
javascript的理解及经典案例分析
May 20 Javascript
jQuery+php实时获取及响应文本框输入内容的方法
May 24 Javascript
js实现非常棒的弹出div
Oct 06 Javascript
使用JavaScript进行表单校验功能
Aug 01 Javascript
使用nvm管理不同版本的node与npm的方法
Oct 31 Javascript
详解vue静态资源打包中的坑与解决方案
Feb 05 Javascript
webpack4与babel配合使es6代码可运行于低版本浏览器的方法
Oct 12 Javascript
Vue 同步异步存值取值实现案例
Aug 05 Javascript
html5 录制mp3音频支持采样率和比特率设置
Jul 15 Javascript
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
ThinkPHP基本的增删查改操作实例教程
2014/08/22 PHP
php array_chunk()函数用法与注意事项
2019/07/12 PHP
用JTrackBar实现的模拟苹果风格的滚动条
2007/08/06 Javascript
javascript+iframe 实现无刷新载入整页的代码
2010/03/17 Javascript
jquery设置text的值示例(设置文本框 DIV 表单值)
2014/01/06 Javascript
Jquery中ajax方法data参数的用法小结
2014/02/12 Javascript
让JavaScript中setTimeout支持链式操作的方法
2015/06/19 Javascript
javascript实现页面滚屏效果
2017/01/17 Javascript
基于javaScript的this指向总结
2017/07/22 Javascript
浅析从vue源码看观察者模式
2018/01/29 Javascript
JavaScript分步实现一个出生日期的正则表达式
2018/03/22 Javascript
Node错误处理笔记之挖坑系列教程
2018/06/05 Javascript
vue实现一拉到底的滑动验证
2019/07/25 Javascript
vue实现列表拖拽排序的功能
2020/11/02 Javascript
[02:24]DOTA2痛苦女王 英雄基础教程
2013/11/26 DOTA
[41:41]TFT vs Secret Supermajor小组赛C组 BO3 第一场 6.3
2018/06/04 DOTA
[53:43]VP vs NewBee Supermajor 胜者组 BO3 第三场 6.5
2018/06/06 DOTA
Python中if __name__ == &quot;__main__&quot;详细解释
2014/10/21 Python
Python contextlib模块使用示例
2015/02/18 Python
Python基础教程之浅拷贝和深拷贝实例详解
2017/07/15 Python
python 禁止函数修改列表的实现方法
2017/08/03 Python
Python 高级专用类方法的实例详解
2017/09/11 Python
Python2中文处理纪要的实现方法
2018/03/10 Python
python编写简易聊天室实现局域网内聊天功能
2018/07/28 Python
python简单操作excle的方法
2018/09/12 Python
python pandas获取csv指定行 列的操作方法
2019/07/12 Python
Django认证系统user对象实现过程解析
2020/03/02 Python
详解python中的闭包
2020/09/07 Python
open_basedir restriction in effect. 原因与解决方法
2021/03/14 PHP
IdealFit官方网站:女性蛋白质、补充剂和运动服装
2019/03/24 全球购物
C#公司笔试题
2014/03/28 面试题
材料成型专业个人求职信范文
2013/09/25 职场文书
公司道歉信范文
2014/01/09 职场文书
中央空调节能方案
2014/06/15 职场文书
经典团队口号大全
2014/06/21 职场文书
小学趣味运动会加油稿
2014/09/25 职场文书