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 相关文章推荐
基于jQuery的前端数据通用验证库
Aug 08 Javascript
javascript中Number对象的toString()方法分析
Dec 20 Javascript
JavaScript实现数组随机排序的方法
Jun 26 Javascript
javascript实现网页子页面遍历回调的方法(涉及 window.frames、递归函数、函数上下文)
Jul 27 Javascript
自定义刻度jQuery进度条及插件
Sep 02 Javascript
javascript的列表切换【实现代码】
May 03 Javascript
Javascript 判断两个IP是否在同一网段实例代码
Nov 28 Javascript
php输出全部gb2312编码内的汉字方法
Mar 04 Javascript
JS中SetTimeout和SetInterval使用初探
Mar 23 Javascript
基于Bootstrap的标签页组件及bootstrap-tab使用说明
Jul 25 Javascript
JS基于正则表达式实现的密码强度验证功能示例
Sep 21 Javascript
vue脚手架中配置Sass的方法
Jan 04 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
PHP实现分页的一个示例
2006/10/09 PHP
Laravel实现表单提交
2017/05/07 PHP
juery框架写的弹窗效果适合新手
2013/11/27 Javascript
介绍JavaScript中Math.abs()方法的使用
2015/06/14 Javascript
JS实现转动随机数抽奖特效代码
2020/04/16 Javascript
AngularJS 遇到的小坑与技巧小结
2016/06/07 Javascript
javascript弹出窗口中增加确定取消按钮
2016/06/24 Javascript
把json格式的字符串转换成javascript对象或数组的方法总结
2016/11/03 Javascript
详解Python中logging日志模块在多进程环境下的使用
2016/12/26 Javascript
聊聊那些使用前端Javascript实现的机器学习类库
2017/09/18 Javascript
基于Vue制作组织架构树组件
2017/12/06 Javascript
JQuery Ajax跨域调用和非跨域调用问题实例分析
2019/04/16 jQuery
vue操作动画的记录animate.css实例代码
2019/04/26 Javascript
JavaScript实现的滚动公告特效【基于jQuery】
2019/07/10 jQuery
详解Vue.js和layui日期控件冲突问题解决办法
2019/07/25 Javascript
node.js使用fs读取文件出错的解决方案
2019/10/23 Javascript
Jquery让form表单异步提交代码实现
2019/11/14 jQuery
JS window对象简单操作完整示例
2020/01/14 Javascript
vue-router的hooks用法详解
2020/06/08 Javascript
python抓取网页时字符集转换问题处理方案分享
2014/06/19 Python
使用python编写监听端
2018/04/12 Python
python批量处理文件或文件夹
2020/07/28 Python
通过实例解析Python调用json模块
2019/12/11 Python
浅谈Django QuerySet对象(模型.objects)的常用方法
2020/03/28 Python
Python读取图像并显示灰度图的实现
2020/12/01 Python
html5教程制作简单画板代码分享
2013/12/04 HTML / CSS
使用索引有什么好处
2016/07/27 面试题
恶意软件的定义
2014/11/12 面试题
献爱心活动总结
2014/05/07 职场文书
九一八事变纪念日演讲稿
2014/09/14 职场文书
党的群众路线剖析材料
2014/10/09 职场文书
2015年食堂工作总结报告
2015/04/23 职场文书
管理者们如何制定2019年的工作计划?
2019/07/01 职场文书
JS ES6异步解决方案
2021/04/29 Javascript
MySQL的join buffer原理
2021/04/29 MySQL
深入浅出讲解Java8函数式编程
2022/01/18 Java/Android