jQuery 1.4给开发者带来了很多值得兴奋的新特性,同时使用jQuery的人也越来越多,为了方便大家对jQuery的使用,下面列出了一些jQuery使用技巧。比如有禁止右键点击、隐藏搜索文本框文字、在新窗口中打开链接、检测浏览器、预加载图片等等。具体如下:

禁止右键点击

  1. $(document).ready(function(){
  2. $(document).bind("contextmenu",function(e){
  3. return false;
  4. });
  5. });

隐藏搜索文本框文字

  1. $(document).ready(function() {
  2. $("input.text1").val("Enter your search text here");
  3. textFill($('input.text1'));
  4. });
  5. function textFill(input){ //input focus text function
  6. var originalvalue = input.val();
  7. input.focus( function(){
  8. if( $.trim(input.val()) == originalvalue ){ input.val(''); }
  9. });
  10. input.blur( function(){
  11. if( $.trim(input.val()) == '' ){ input.val(originalvalue); }
  12. });
  13. }

在新窗口中打开链接

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

检测浏览器

注: 在版本jQuery 1.4中,$.support 替换掉了$.browser 变量。

  1. $(document).ready(function() {
  2. // Target Firefox 2 and above
  3. if ($.browser.mozilla && $.browser.version >= "1.8" ){
  4. // do something
  5. }
  6. // Target Safari
  7. if( $.browser.safari ){
  8. // do something
  9. }
  10. // Target Chrome
  11. if( $.browser.chrome){
  12. // do something
  13. }
  14. // Target Camino
  15. if( $.browser.camino){
  16. // do something
  17. }
  18. // Target Opera
  19. if( $.browser.opera){
  20. // do something
  21. }
  22. // Target IE6 and below
  23. if ($.browser.msie && $.browser.version <= 6 ){
  24. // do something
  25. }
  26. // Target anything above IE6
  27. if ($.browser.msie && $.browser.version > 6){
  28. // do something
  29. }
  30. });

预加载图片

  1. $(document).ready(function() {
  2. jQuery.preloadImages = function()
  3. {
  4. for(var ; i").attr("src", arguments[i]);
  5. }
  6. };
  7. // how to use
  8. $.preloadImages("image1.jpg");
  9. });

页面样式切换

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

原文地址:http://www.cnblogs.com/xiaopin/archive/2010/11/13/1876366.html

Jquery常用功能的更多相关文章

  1. JQuery常用功能的性能优化

    使用最佳选择器 通常比较常用的选择器有以下几个: 1.ID选择器 $("#id") 2.标签选择器 $("td") 3.类选择器 $(".target ...

  2. 从零开始学习jQuery (十) jQueryUI常用功能实战

    一.摘要 本系列文章将带您进入jQuery的精彩世界, 其中有很多作者具体的使用经验和解决方案,  即使你会使用jQuery也能在阅读中发现些许秘籍. 本文是实战篇. 使用jQueryUI完成制作网站 ...

  3. JQuery常用函数及功能

    JQuery常用函数及功能小结 来源:http://blog.csdn.net/screensky/article/details/7831000 1.文档加载完成执行函数 $(document).r ...

  4. [转]WebPack 常用功能介绍

    概述 Webpack是一款用户打包前端模块的工具.主要是用来打包在浏览器端使用的javascript的.同时也能转换.捆绑.打包其他的静态资源,包括css.image.font file.templa ...

  5. JQuery常用实用的事件[较容易忽略的方法]

     JQuery常用实用的事件 注:由于JQuery片段较多就没有用插入代码文本插入,请见谅!JQuery 事件处理ready(fn)代码: $(document).ready(function(){ ...

  6. JQuery常用API 核心 效果 JQueryHTML 遍历 Event事件

    JQuery 常用API 参考资料:JQuery 官网   jQuery API 中文文档 核心 jQuery 对象 jQuery() 返回匹配的元素集合,无论是通过在DOM的基础上传递的参数还是创建 ...

  7. 非常好用的弹出层 layer,常用功能demo,快速上手!

    功能强大,实用,操作方便,文档齐全. 参数灵活,丰富.可以作为开发项目的公共模块,多处使用.老文档地址:http://layer.layui.com/api.html 已经停止维护 新文档地址:htt ...

  8. js与jquery常用数组方法总结

    昨天被问数组方法的时候,问到sort()方法是否会改变原来的数组.本来我猜是不会,也是这么说,马上我又觉得,知识这种东西,不确定的时候直接说不确定或不知道就好,只是凭借着不确定的猜测或者是记忆,害人害 ...

  9. Atiit 常见功能 常用功能与模块的最快速解决方案

    Atiit 常见功能 常用功能与模块的最快速解决方案 一.大力使用第三方API接口 一.导出excel jquery.table2excel 二.Form表单验证,使用h5验证属性 验证发生在form ...

随机推荐

  1. JAVA基础英语单词表(中)

    factory                                 / 'fæktəri /                    工厂 fetch                     ...

  2. 几种不同风格的Toast

    一般情况下,我们使用Toast默认的风格就行了,只是有些时候为了达到我们自己想要的效果需要自定义一下,包括自定义显示的位置,显示的内容以及完全自定义里面的布局,代码如下: activity: pack ...

  3. CentOS源码安装搭建LNMP全过程(包括nginx,mysql,php,svn)【转】

    转自:http://blog.csdn.net/yanzi1225627/article/details/49123659 服务器环境为:CentOS6.5 64位 目标:搭建LNMP(Linux + ...

  4. 20169210《Linux内核原理与分析》第九周作业

    第一部分:实验 首先还是网易云课堂的学习,这次的课程是可执行程序的装载. 预处理.编译和链接: 可执行程序是怎么来的 以c语言代码为例的话,经过预处理,编译成汇编代码,再汇编成目标码再链接可执行文件. ...

  5. 绘制3D的托卡马克位形图的matlab脚本文件 ThreeD.m

    % 绘制3D的托卡马克位形图, (V 0.1 by Jiale Chan for Y. H. Huang) % Dee Formula % 特征参数     rzero = 2.0;     rmax ...

  6. [ES7] Descorator: evaluated & call order

    When multiple decorators apply to a single declaration, their evaluation is similar to function comp ...

  7. Linux系统下UDP发送和接收广播消息小样例

    [cpp] view plaincopy // 发送端 #include <iostream> #include <stdio.h> #include <sys/sock ...

  8. TCP 连接的建立和终止

    三路握手 建立一个TCP连接时会发生下述情形. (1)服务器必须准备好接受外来的连接.这通常通过调用socket.bind和listen这3个函数来完成的,我们称之为被动打开. (2)客户通过调用co ...

  9. The content of element type "beans" must match "(description?,(import|alias|bean)*)

    The content of element type "beans" must match "(description?,(import|alias|bean)*) - ...

  10. div垂直居中的方法(转)

    在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支持我只需做少许的CSS Hack技术就可以啊!所以在这里我还要啰嗦两句,CSS中 ...