`

5 Helpful jQuery Tricks(五个有用的jQuery技巧)

阅读更多

在开始之前,我们简单的介绍一下到底什么是jQuery

jQuery是一个优秀的Javascript框架。它是快速的,简洁的,轻量级的js库,使用户能够更方便地处理HTML documentsevents、实现动画效果,并且方便地为网站提供AJAX交互。它能够兼容各种浏览器,一直坚持的核心理念是WRITE LESSDO MORE(写的更少,做的更多)。

上面的话看上去很官方,但是这是我们必须要了解的。jQuery能够实现很多的页面效果,下面的五个技巧是我们经常要用到的,所以简单的给大家翻译一下,供大家一起学习吧。

1.改变字体的大小

允许用户调整网站字体的大小具有很多的优点,可以更好的提高用户体验。下面使用jQuery给大家展示一下如何使用它。

//check that the DOM is ready
$(document).ready(function() {
	//get the current font size
	var originalFontSize = $('html').css('font-size');

	//Increase the font size
	$(".increaseFont").click(function() {
		var currentFontSize = $('html').css('font-size');
		var currentFontSizeNumber = parseFloat(currentFontSize, 10);
		//increases the font- could be set to a value from 
		//the user as well
		var newFontSize = currentFontSizeNumber*1.2;
		$('html').css('font-size', newFontSize);
		return false;
	});

	//Decrease the Font Size
	$(".decreaseFont").click(function() {
		var currentFontSize = $('html').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		//decreases font.  Could be set to a value from
		//the user as well
		var newFontSize = currentFontSizeNum*0.8;
		$('html').css('font-size', newFontSize);
		return false;
	});

	// Reset Font Size
	$(".resetFont").click(function(){
	$('html').css('font-size', originalFontSize);
  });
});

2.在新窗口中打开链接

也许你跟我一样,在打开外部链接时不喜欢多次验证就可以实现。但是在XHTML 1.0中又没有“_blank”的标签属性。并且网站的拥有者也想在新窗口中打开所有外部链接时,能够保持访问者仍然在自己的网站上。就这jQuery而言,将允许在新窗口中打开所有的外部链接。

//check that the DOM is ready
$(document).ready(function() {
	//select all anchor tags that have http in the href
	//and apply the target=_blank
	$("a[href^='http']").attr('target','_blank');
});

好了现在,所有的外部链接将可以打开一个新窗口,允许用户留在原页面。如果你使用了大量的外部文档链接如PDFDOC文件可以创建一些规则在新窗口中来加载这些文件。

3.互换样式表    

不仅允许用户改变页面字体大小,而且您还允许用户能够选择不同的主题样式。

//check that the DOM is ready
$(document).ready(function() {
       $("a.cssSwap").click(function(){ 
                //swapthe link rel attributewith the value in the rel    

               $('link[rel=stylesheet]').attr('href', $(this).attr('rel'));
       }); 
});

最后亲自体验一下互换样式表是怎么工作的。

4. 禁用右键

通常禁用右键是为了防止用户直接复制页面的信息或者是你想创建一个自己的独特的右键功能,下面我们来看一下是如何实现的:

//check that the DOM is ready

$(document).ready(function() {
       //catch the right-click contextmenu
       $(document).bind("contextmenu",function(e){                              

                //warningprompt - optional
               alert("Noright-clicking!");
                                       

                //cancelthe default contextmenu
                returnfalse;
       });
});

jQuery能够更简单、容易的处理右键的单击事件。

5. 返回顶部链接

对于大篇幅的页面,可以通过增加“返回顶部”的链接来使用户方便地返回到页面顶部。这是一个简单的JavaScript实现的效果,我们也可以通过jQuery运用滚动效果增添一点点小技巧。

$(document).ready(function() {
	//when the id="top" link is clicked
	$('#top').click(function() {
		//scoll the page back to the top
		$(document).scrollTo(0,500);
	}
});

对于大篇幅的页面来说,这是一个必备的功能。

当你想成为熟练掌握jQuery网页开发者,一定会发现更多的jQuery的使用技巧。最后一句,用我的话说:“革命尚未成功,同志仍需努力。”

这是我第一次翻译文章,好困难啊,在兴奋中开始,在磕磕绊绊中结束,哎,真是:革命尚未成功,同志仍需努力

 

英语原文地址:

http://www.learncomputer.com/5-helpful-jquery-tricks/

分享到:
评论

相关推荐

    26个JQuery使用技巧

    The use of the jQuery library is growing and growing(just released jQuery 1.4), more and more people are using this useful javascript library. This means that more and more useful jQuery tips, ...

    jQuery- Novice to Ninja

    jQuery: Novice to Ninja will show you how to unleash the amazing power of jQuery. In this easy-to-follow guide, you'll master all the major tricks and techniques that jQuery offers-within hours.

    《jQuery: Novice to Ninja: New Kicks And Tricks》英文原版 Kindle - mobi格式

    jQuery: Novice to Ninja: New Kicks And Tricks (2nd Edition) is your fast track to mastering jQuery—the all-conquering JavaScript framework. Used by over half the world’s top 10,000 websites, jQuery ...

    CTF技巧集合 tricks

    Phithon——CTF比赛总是输?你还差点Tricks!非常有用的资源

    HTML5结合jQuery实现图片滑块特效 绚丽jQuery焦点图

    对于jQuery,我们在html5tricks上分享的相对比较少,大家可以去jQuery插件栏目中查看自己需要的jQuery插件。今天我们要来分享一款很酷的jQuery焦点图插件,它带有一个导航菜单,它结合HTML5和CSS3技术,使这个焦点图...

    jQuery UI Cookbook (pdf + ePub)

    Benefits and tricks in each jQuery UI widget Build your own widget using the widget factory and the theme framwork Built-in event-handling utilities for inter-widget communication Extend widgets to ...

    《jQuery: Novice to Ninja》- 2017 英文原版

    Note: the latest edition of this book is jQuery: Novice to Ninja: New Kicks And Tricks jQuery: Novice to Ninja is your fast track to mastering jQuery—the all-conquering JavaScript framework. Used by...

    Jquery下的26个实用小技巧(jQuery tips, tricks solutions)

    比如有禁止右键点击、隐藏搜索文本框文字、在新窗口中打开链接、检测浏览器、预加载图片、页面样式切换、所有列...个数、使用Google主机上的Jquery类库、禁用Jquery效果、解决Jquery类库与其他Javascript类库冲突问题...

    jquery操作asp.net中GridView方法

    用户可通过:http://www.jquerybyexample.net/2013/07/jquery-gridview-aspnet-tips-and-tricks.html 下载 Formatting Related Formatting ASP.NET GridView using jQuery Highlight row on mouseover in ...

    Jquery下的26个实用小技巧(jQuery tips, tricks & solutions)

    前段时间发布了Jquery类库1.4版本,使用者也越来越多,为了方便大家对Jquery的使用,下面列出了一些Jquery使用技巧。

    图像分类训练中的 tricks

    针对图像分类任务,提升准确率的方法路线有两条,一个是模型的修改,另一个是各种数据处理和训练的技巧(tricks)。图像分类中的各种技巧对于目标检测、图像分割等任务也有很好的作用,因此值得好好总结。本文在精读...

    Neural Networks—Tricks of the Trade (2nd Edition).pdf

    This book is an outgrowth of a 1996 NIPS workshop called Tricks of the Trade whose goal was to begin the process of gathering and documenting these tricks. The interest that the workshop generated, ...

    vvvv_tricks几个V4的功能技巧Demo

    vvvv是款神奇的交互软件,您不需编程基础,就可通过它简单的图形化编辑,能够实现大型媒体与物理接口互通,实时动态图像演示,音频和视频处理,虚拟人机互动...包中几个vvvv小Demo可以帮助您拓展节点运用的巧妙功能。

    jQuery:jQuery学习路径源代码-jquery source code

    该存储库包含文章的源代码,这些文章是Dot Net Tricks jQuery学习路径的一部分。 初学者 中间的 先进的 jQuery Ajax方法 在本文中,我们将了解jQuery中的ajax功能,以及如何使用它从服务器获取数据并将数据发布到...

    Frontend Tutorials and Tricks — 前端技巧和教程.zip

    Frontend Tutorials and Tricks — 前端技巧和教程.zip

    分享10款效果惊艳的HTML5图片特效

    对于jQuery,我们在html5tricks上分享的相对比较少,大家可以去jQuery插件栏目中查看自己需要的jQuery插件。今天我们要来分享一款很酷的jQuery焦点图插件,它带有一个导航菜单,它结合HTML5和CSS3技术,使这个焦点图...

    S7-200使用技巧(tips&tricks;).rar

    S7-200使用技巧(tips&tricks;)rar,S7-200使用技巧(tips&tricks;)

    python tricks

    Python_Tricks_-_A_Buffet_of_Awesome_Python_Features

    anti-unpacker tricks 反脱壳的技巧

    anti-unpacker tricks是学习脱壳软件加密解密软件破解的基础。。

Global site tag (gtag.js) - Google Analytics