分享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 相关文章推荐
js中substring和substr的详细介绍与用法
Aug 29 Javascript
node.js中的http.response.writeHead方法使用说明
Dec 14 Javascript
JavaScript入门系列之知识点总结
Mar 24 Javascript
JavaScript代码里的判断小结
Aug 22 Javascript
React Router基础使用
Jan 17 Javascript
js获取当前页的URL与window.location.href简单方法
Feb 13 Javascript
jQuery插件zTree实现清空选中第一个节点所有子节点的方法
Mar 08 Javascript
微信小程序实现页面跳转传值的方法
Oct 12 Javascript
vue mint-ui 实现省市区街道4级联动示例(仿淘宝京东收货地址4级联动)
Oct 16 Javascript
jQuery实现打开网页自动弹出遮罩层或点击弹出遮罩层功能示例
Oct 19 jQuery
vue项目中常见问题及解决方案(推荐)
Oct 21 Javascript
element-ui中dialog弹窗关闭按钮失效的解决
Sep 22 Javascript
详解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
php数据结构 算法(PHP描述) 简单选择排序 simple selection sort
2011/08/09 PHP
javascript高亮效果的二种实现方法
2008/09/14 Javascript
该如何加载google-analytics(或其他第三方)的JS
2010/05/13 Javascript
javascript完美拖拽的实现方法
2013/09/29 Javascript
Js实现无刷新删除内容
2015/04/29 Javascript
jquery实现select选择框内容左右移动代码分享
2015/11/21 Javascript
快速移动鼠标触发问题及解决方法(ECharts外部调用保存为图片操作及工作流接线mouseenter和mouseleave)
2016/08/29 Javascript
jQuery中的insertBefore(),insertAfter(),after(),before()区别介绍
2016/09/01 Javascript
利用jquery实现瀑布流3种案例
2016/09/18 Javascript
JS实现微信弹出搜索框 多条件查询功能
2016/12/13 Javascript
微信小程序 定位到当前城市实现实例代码
2017/02/23 Javascript
详解vue2.0的Element UI的表格table列时间戳格式化
2017/06/13 Javascript
详解JS构造函数中this和return
2017/09/16 Javascript
Vue实现购物车场景下的应用
2017/11/27 Javascript
js中getBoundingClientRect的作用及兼容方案详解
2018/02/01 Javascript
使用jquery模拟a标签的click事件无法实现跳转的解决
2018/12/04 jQuery
深入浅析Vue 中 ref 的使用
2019/04/29 Javascript
如何实现小程序tab栏下划线动画效果
2019/05/18 Javascript
通过实例解析javascript Date对象属性及方法
2020/11/04 Javascript
编写v-for循环的技巧汇总
2020/12/01 Javascript
[01:42]TI4西雅图DOTA2前线报道 第一顿早饭哦
2014/07/08 DOTA
[09:13]DOTA2-DPC中国联赛 正赛 Ehome vs Magma 选手采访 1月19日
2021/03/11 DOTA
Python中序列的修改、散列与切片详解
2017/08/27 Python
Python实现截取PDF文件中的几页代码实例
2019/03/11 Python
Django 全局的static和templates的使用详解
2019/07/19 Python
使用Keras加载含有自定义层或函数的模型操作
2020/06/10 Python
python如何停止递归
2020/09/09 Python
基于python实现坦克大战游戏
2020/10/27 Python
详解纯CSS3制作的20种loading动效
2017/07/05 HTML / CSS
使用CSS3配合IE滤镜实现渐变和投影的效果
2015/09/06 HTML / CSS
moosejaw旗下的户外商品促销网站:Mountain Steals
2017/02/27 全球购物
漫威玩具服装及周边商品官方购物网站:Marvel Shop
2019/05/11 全球购物
紫日观后感
2015/06/05 职场文书
python3操作redis实现List列表实例
2021/08/04 Python
《仙剑客栈2》第一弹正式宣传片公开 年内发售
2022/04/07 其他游戏
NASA 机智号火星直升机拍到了毅力号设备碎片
2022/04/29 数码科技