1. 禁止右键点击

$(document).ready(function(){
  $(document).bind("contextmenu",function(e){
    return false;
  });
});
 
2. 隐藏搜索文本框文字
$(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.在新窗口中打开链接

$(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.opensourcehunter.com" rel=external>open link</a>
 
4. 检测浏览器
$(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. 预加载图片
$(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[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>
});
 
7. 获得鼠标指针XY值
$(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>
});
 
8.检查图片是否加载完成
有时候你需要确保图片完成加载完成以便执行后面的操作:
$('img').load(function () {
 console.log('image load successful');
});
 
9.自动修改破损图像
如果你碰巧在你的网站上发现了破碎的图像链接,你可以用一个不易被替换的图像来代替它们。添加这个简单的代码可以节省很多麻烦:
$('img').on('error', function () {
 $(this).prop('src', 'img/broken.png');
});
 
10. jQuery延时加载功能
$(document).ready(function() {
  window.setTimeout(function() {
   // do something
  }, 1000);
});

分享jQuery封装好的一些常用操作的更多相关文章

  1. jQuery对select标签的常用操作

    1.获取当前选中项的value. $("#selector").val(); 2.获取当前选中项的text. $("#selector").find(" ...

  2. mysql的常用操作的封装

    1.概述: 为了把繁琐的操作简化成简单的类,设计了2个类用来封装了mysql的常用操作,以便使用者可以方便地使用. 2.组成 1)数据库操作类CDatabaseConnect 2)SQL对象类CSql ...

  3. js,jQuery数组常用操作小结

    一.js中数组常用操作小结 (1) shift:删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4,5]; var b = a.shift() ...

  4. Ajax-04 jQuery Ajax 常用操作

    jQuery jQuery 其实就是一个JavaScript的类库,其将复杂的功能做了上层封装,使得开发者可以在其基础上写更少的代码实现更多的功能. jQuery Ajax a.概述 jQuery 不 ...

  5. 自己使用Jquery封装各种功能分享

    自己使用Jquery封装各种功能分享: 左右滚动图片 瀑布流 流动显示列表 广告切换 头像切换And广告切换 获取搜索引擎的来源关键字 上面列表中展示的功能都是使用jquery进行封装实现的,希望大家 ...

  6. .Net Excel操作之NPOI(二)常用操作封装

    一.Excel数据导出常用操作 1.指定表头和描述 2.指定数据库中读出的数据集合 二.ExcelExport封装 /// <summary> /// Excel常用的表格导出逻辑封装 / ...

  7. jQuery常用操作

    jQuery jQuery是一个轻量级的JS库,是一个被封装好的JS文件,提供了更为简便的元素操作方式,jQuery封装了DOM. 使用jQuery 引入jQuery文件 <scrtipt sr ...

  8. JQuery常用操作实现方式

    http://www.cnblogs.com/guomingfeng/articles/2038707.html 一个优秀的 JavaScript 框架,一篇 jQuery 常用方法及函数的文章留存备 ...

  9. nodejs 进阶:封装所有对数据库的常用操作

    //这个模块里面封装了所有对数据库的常用操作 var MongoClient = require('mongodb').MongoClient; var settings = require(&quo ...

随机推荐

  1. PHP函数之类

    if(isset($_POST['dosubmit'])) { } 打散 $array_urls = explode("\r\n",$inurl_str); foreach ($a ...

  2. Wamp错误: Forbidden You don't have permission to access / on this server.

    找到php的配置文件httpd.conf(找不到的话看这篇:http://www.cnblogs.com/liulangmao/p/3569807.html) 在原有的位置文件中找到配置节 <D ...

  3. Linux写时拷贝技术(copy-on-write)

    1.传统的fork()函数创建一个子进程,子进程和父进程共享正文段,复制数据段,堆,栈到子进程示意图如下: 2.Linux的fork()函数-写时复制(copy-on-write)创建一个子进程,内核 ...

  4. mycli---数据库工具(提示、自动补全)

    前言 朋友介绍了一个工具,mycli,支持MySQL查询语句自动补全等,这里给大家介绍一下. 大家也可以直接去官网看一下,安装使用都很简单. 安装 $ pip install -U mycli $ b ...

  5. java.lang.NoClassDefFoundError: Ljavax/enterprise/inject/spi/BeanManager;

    java.lang.NoClassDefFoundError: Ljavax/enterprise/inject/spi/BeanManager; 删除Struts2-cdi-plug 的插件的jar ...

  6. awk打印指定列以后的所有内容

    (1)使用awk将文件的前12列替换为空 awk '{for(i=1;i<=12;i++)$i="";print $0}' localhost_access_log //写法 ...

  7. Python IDLE快捷键【转载合集】

    转载自:http://www.douban.com/note/212321426/ 编辑状态时:Ctrl + [ .Ctrl + ] 缩进代码Alt+3 Alt+4 注释.取消注释代码行Alt+5 A ...

  8. FreeRTOS基础篇教程目录汇总

    以下教程(大部分章节)(尤其理论介绍部分)转载自安富莱电子,官网链接: http://forum.armfly.com/forum.php 然后根据安富莱的教程自己做了分析和测试,希望大家共同进步. ...

  9. word公式编辑器公式

    Linear format equations and Math AutoCorrect in Word Applies To: Word 2016 Outlook 2016 Word 2013 Ou ...

  10. Servlet入门(第一个Servlet的Web程序)

    新建maven项目,注意项目的类型 project名为ServletExample 点击Finish.建立maven项目完毕例如以下 生成后的文件夹没有java源代码文件夹.依照maven的约定,还要 ...