7个有用的jQuery代码片段分享
jQuery是一款轻量级的JavaScript库,是最流行的客户端HTML脚本之一,它在WEB设计师和开发者中非常的有名,并且有非常多有用的插件和技术帮助WEB开发人员开发出有创意和漂亮的WEB页面。
今天我们为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'); });
二、设置等高的列
应用下面的代码,可以使得你的WEB应用每一列高度都想等:
<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…'); });
四、禁用鼠标右键
$(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; }); });
五、设定计时器
$(document).ready(function() { window.setTimeout(function() { // some code }, 500); });
六、计算子元素的个数
<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>
七、把元素定位到页面中间
<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>
相关文章
jQuery easyui datagrid动态查询数据实例讲解
接下来将从前台提交查询条件,从MSSQL返回json数据的一个事例来讲解一下datagrid动态查询数据,感兴趣的你可不要错过了哈,希望本文可以帮助到你2013-02-02javascript(基于jQuery)实现鼠标获取选中的文字示例【测试可用】
这篇文章主要介绍了javascript(基于jQuery)实现鼠标获取选中的文字,涉及jQuery响应鼠标事件及页面元素属性动态操作相关实现技巧,需要的朋友可以参考下2019-10-10JQuery中attr属性和jQuery.data()学习笔记【必看】
下面小编就为大家带来一篇JQuery中attr属性和jQuery.data()学习笔记【必看】。小编觉得挺不错的,现在分享给大家,也给大家做个参考,一起跟随小编过来看看吧2016-05-05jquery 动态增加,减少input表单的简单方法(必看)
下面小编就为大家带来一篇jquery 动态增加,减少input表单的简单方法(必看)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2016-10-10jQuery.position()方法获取不到值的安全替换方法
这篇文章主要介绍了jQuery.position()方法获取不到值的安全替换方法,本文给出了一种变通的方法,用.offset()来换算,需要的朋友可以参考下2015-03-03jQuery获得IE版本不准确webbrowser的解决方法
用$.browser.version经常出现一些不准确的状况,最近碰到,做了一些总结,不知道是否全面2014-02-02
最新评论