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 Jcrop插件实现图片选取功能
Nov 23 Javascript
JavaScript基础语法让人疑惑的地方小结
May 23 Javascript
JavaScript之编码规范 推荐
May 23 Javascript
JavaScript动态修改网页元素内容的方法
Mar 21 Javascript
jQuery-1.9.1源码分析系列(十)事件系统之事件体系结构
Nov 19 Javascript
jQuery图片瀑布流的简单实现代码
Mar 15 Javascript
基于three.js编写的一个项目类示例代码
Jan 05 Javascript
React中this丢失的四种解决方法
Mar 12 Javascript
vue-router两种模式区别及使用注意事项详解
Aug 01 Javascript
javascript简单实现深浅拷贝过程详解
Oct 08 Javascript
vuex+axios+element-ui实现页面请求loading操作示例
Feb 02 Javascript
react合成事件与原生事件的相关理解
May 13 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
解决dede生成静态页和动态页转换的一些问题,及火车采集入库生成动态的办法
2007/03/29 PHP
PHP的preg_match匹配字符串长度问题解决方法
2014/05/03 PHP
codeigniter框架The URI you submitted has disallowed characters错误解决方法
2014/05/06 PHP
php 读写json文件及修改json的方法
2018/03/07 PHP
服务端 VBScript 与 JScript 几个相同特性的写法 By shawl.qiu
2007/03/06 Javascript
关于用Jquery的height()、width()计算动态插入的IMG标签的宽高的问题
2010/12/08 Javascript
js实现选中复选框文字变色的方法
2015/08/14 Javascript
JQuery的Pager分页器实现代码
2016/05/03 Javascript
深入理解JQuery循环绑定事件
2016/06/02 Javascript
浅谈JavaScript 浏览器对象
2016/06/03 Javascript
自己封装的一个原生JS拖动方法(推荐)
2016/11/22 Javascript
微信小程序 textarea 组件详解及简单实例
2017/01/10 Javascript
jQuery插件FusionCharts实现的Marimekko图效果示例【附demo源码】
2017/03/24 jQuery
微信web端后退强制刷新功能的实现代码
2018/03/04 Javascript
mpvue开发音频类小程序踩坑和建议详解
2019/03/12 Javascript
vue 源码解析之虚拟Dom-render
2019/08/26 Javascript
微信JSSDK实现打开摄像头拍照再将相片保存到服务器
2019/11/15 Javascript
extjs图形绘制之饼图实现方法分析
2020/03/06 Javascript
如何在vue中使用jointjs过程解析
2020/05/29 Javascript
vue设置全局访问接口API地址操作
2020/08/14 Javascript
jQuery实现图片切换效果
2020/10/19 jQuery
如何实现小程序与小程序之间的跳转
2020/11/04 Javascript
python制作填词游戏步骤详解
2019/05/05 Python
Python 获取ftp服务器文件时间的方法
2019/07/02 Python
详解pyinstaller selenium python3 chrome打包问题
2019/10/18 Python
pycharm修改file type方式
2019/11/19 Python
英国最大的在线运动补充剂商店:Discount Supplements
2017/06/03 全球购物
Joules美国官网:出色的英国风格
2017/10/30 全球购物
饿了么订餐官网:外卖、网上订餐
2019/06/28 全球购物
学生请假条格式
2014/04/11 职场文书
文明礼仪标语
2014/06/13 职场文书
会计试用期自我评价怎么写
2014/09/18 职场文书
公司开业的祝贺语大全(60条)
2019/07/05 职场文书
《我在为谁工作》:工作的质量往往决定生活的质量
2019/12/27 职场文书
Html5调用企业微信的实现
2021/04/16 HTML / CSS
vscode中使用npm安装babel的方法
2021/08/02 Javascript