分享12个实用的jQuery代码片段


Posted in Javascript onMarch 09, 2016

jQuery是一款优秀的JavaScript库,它在WEB开发者和网页设计师中非常出名,帮助网页设计师开发出很多有创意和漂亮的WEB页面。

本文主要分享了12个有用的jQuery技巧,具体内容如下

下面是我收集的一些小技巧,这些技巧将帮助你提高你网站布局和应用的创意性以及功能性。

一、在新窗口打开链接

用这个代码,你点击超文本链接时会在新窗口中打开,为用户带来更好的体验:

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

二、设定计时器

$(document).ready(function() {
 window.setTimeout(function() {
 // some code
 }, 500);
});

 

三、设置等高的列

使用下面的代码,可以使得你的网页应用每一列高度都一样:

<div class="equalHeight" style="border:1px solid">
 <p>First Line</p>
 <p>Second Line</p>
 <p>Third Line</p>
</div>
<div class="equalHeight" style="border:1px solid">
 <p>Column Two</p>
</div>
<script src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
 equalHeight('.equalHeight');
});
//global variable, this will store the highest height value
var maxHeight = 0;
function equalHeight(col) {
 //Get all the element with class = col
 col = $(col);
 //Loop all the col
 col.each(function() {
 //Store the highest value
 if ($(this).height() > maxHeight) {
 maxHeight = $(this).height();
 }
 });
 //Set the height
 col.height(maxHeight);
}
</script>

 四、jQuery预加载图像

这个技巧可以加快网页加载图片的速度:

jQuery.preloadImagesInWebPage = function() {
 for (var ctr = 0; ctr & lt; arguments.length; ctr++) {
 jQuery("").attr("src", arguments[ctr]);
 }
}
// 使用方法:
$.preloadImages("image1.gif", "image2.gif", "image3.gif");
// 检查图片是否被加载
$('#imageObject').attr('src', 'image1.gif').load(function() {
 alert('The image has been loaded…');
});

 五、把元素定位到页面中间

<div id="foo" style="width:200px;height: 200px;background: #ccc;"></div>
<script src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
jQuery.fn.center = function() {
 this.css("position", "absolute");
 this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
 this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
 return this;
}
//Use the above function as:
$('#foo').center();
</script>

六、禁用鼠标右键

$(document).ready(function() {
 //catch the right-click context menu
 $(document).bind("contextmenu", function(e) {
 //warning prompt - optional
 alert("No right-clicking!");
 //delete the default context menu
 return false;
 });
});

 七、计算子元素的个数

<div id="foo">
 <div id="bar"></div>
 <div id="baz">
 <div id="biz"></div>
 <span><span></span></span>
 </div>
</div>
<script src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
 //jQuery code to count child elements $("#foo > div").size()
alert($("#foo > div").size())
</script>

 八、更改样式列表

这段代码将帮助你更改样式列表。

$(document).ready(function() {
 $("a.cssSwap").click(function() { 
 //swap the link rel attribute with the value in the rel 
 $('link[rel=stylesheet]').attr('href' , $(this).attr('rel')); 
 }); 
 });

 九、使用jQuery设定文本大小

加入这段代码,用户可根据需求重新设定文本尺寸(增加或减少)。

$(document).ready(function() {
 //find the current font size
 var originalFontSize = $('html').css('font-size');

//Increase the text size
 $(".increaseFont").click(function() {
 var currentFontSize = $('html').css('font-size');
 var currentFontSizeNumber = parseFloat(currentFontSize, 10);

var newFontSize = currentFontSizeNumber*1.2;
 $('html').css('font-size', newFontSize);
 return false;
 });

//Decrease the Text Size
 $(".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;
 });

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

十、检测当前鼠标坐标

使用这个JS代码,你可以在任何浏览器里获取鼠标坐标。

$(document).ready(function() {
 $().mousemove(function(e)
 {
 $('# MouseCoordinates ').html("X Axis Position = " + e.pageX + " and Y Axis Position = " + e.pageY);
 });
 

十一、获取鼠标指针的X / Y轴

$().mousemove(function(e){
 //display the x and y axis values inside the P element
 $('p').html("X Axis : " + e.pageX + " | Y Axis " + e.pageY);
 });

 十二、返回到顶部链接

此代码对于比较长的页面非常实用,用户不必拉滚动条来返回顶部,直接点击“返回顶部”即可。

$(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是JavaScript最好的库之一,支持Ajax及HTML 脚本客户端,主要用于事件处理及制作动画。另外,jQuery还拥有各种插件,可以让开发者在最快的时间内创建网页。

希望本文所述对大家学习jquery程序设计有所帮助。

Javascript 相关文章推荐
jQuery 加上最后自己的验证
Nov 04 Javascript
jQuery EasyUI API 中文文档 - ProgressBar 进度条
Sep 29 Javascript
文本框根据输入内容自适应高度的代码
Oct 24 Javascript
JavaScript语言核心数据类型和变量使用介绍
Aug 23 Javascript
jquery自定义右键菜单、全选、不连续选择
Mar 01 Javascript
JavaScript  cookie 跨域访问之广告推广
Apr 20 Javascript
JavaScript实现经典排序算法之冒泡排序
Dec 28 Javascript
vue移动端实现红包雨效果
Jun 23 Javascript
angular-tree-component的使用详解
Jul 30 Javascript
VueCli3.0中集成MockApi的方法示例
Jul 05 Javascript
vue实现户籍管理系统
May 29 Javascript
VUE中的v-if与v-show区别介绍
Mar 13 Vue.js
详解JavaScript正则表达式之分组匹配及反向引用
Mar 09 #Javascript
javascript html5移动端轻松实现文件上传
Mar 27 #Javascript
javascript事件绑定学习要点
Mar 09 #Javascript
js实现分割上传大文件
Mar 09 #Javascript
js实现ctrl+v粘贴上传图片(兼容chrome、firefox、ie11)
Mar 09 #Javascript
基于jQuery的网页影音播放器jPlayer的基本使用教程
Mar 08 #Javascript
利用jQuery设计一个简单的web音乐播放器的实例分享
Mar 08 #Javascript
You might like
模拟xcopy的函数
2006/10/09 PHP
jQuery+PHP实现的掷色子抽奖游戏实例
2015/01/04 PHP
使用PHP接受文件并获得其后缀名的方法
2015/08/05 PHP
PHP验证码无法显示的原因及解决办法
2017/08/11 PHP
解决PHP curl或file_get_contents下载图片损坏或无法打开的问题
2019/10/11 PHP
javascript编程起步(第六课)
2007/02/27 Javascript
js的2种继承方式详解
2014/03/04 Javascript
捕获和分析JavaScript Error的方法
2014/03/25 Javascript
nodejs获取本机内网和外网ip地址的实现代码
2014/06/01 NodeJs
css与javascript跨浏览器兼容性总结
2014/09/15 Javascript
TypeError document.getElementById(...) is null错误原因
2015/05/18 Javascript
js微信支付实现代码
2016/12/22 Javascript
JS实现JSON.stringify的实例代码讲解
2017/02/07 Javascript
Vue仿今日头条实例详解
2018/02/06 Javascript
ES6知识点整理之Proxy的应用实例详解
2019/04/16 Javascript
在vue中使用echarts(折线图的demo,markline用法)
2020/07/20 Javascript
简单讲解Python中的闭包
2015/08/11 Python
python中logging库的使用总结
2017/10/18 Python
简单了解python模块概念
2018/01/11 Python
python利用高阶函数实现剪枝函数
2018/03/20 Python
Python使用random模块生成随机数操作实例详解
2019/09/17 Python
关于Python turtle库使用时坐标的确定方法
2020/03/19 Python
Python3.7 读取音频根据文件名生成脚本的代码
2020/04/07 Python
使用matplotlib动态刷新指定曲线实例
2020/04/23 Python
jupyter notebook oepncv 显示一张图像的实现
2020/04/24 Python
Python 如何对文件目录操作
2020/07/10 Python
你的自行车健身专家:FaFit24
2016/11/16 全球购物
实现向右循环移位
2014/07/31 面试题
环境工程专业个人求职信
2013/12/05 职场文书
好邻里事迹材料
2014/01/16 职场文书
财务学生的职业生涯发展
2014/02/11 职场文书
装修协议书范本
2014/04/21 职场文书
区域销售主管岗位职责
2014/06/15 职场文书
会计试用期工作总结2015
2015/05/28 职场文书
导游词之上海杜莎夫人蜡像馆
2019/11/22 职场文书
MySQL面试题讲解之如何设置Hash索引
2021/11/01 MySQL