七个不允许错过的jQuery小技巧
作者:bea
jQuery是一款轻量级的JavaScript库,是最流行的客户端HTML脚本之一,它在WEB设计师和开发者中非常的有名,并且有非常多有用的插件和技术。 本文我们将为大家分享一些jQuery小技巧: 一、在新窗口打开链接 用下面的代码,你点击链接即可在新窗口打开: $(document).ready(function() { //select all anchor tags that have http in the href //and apply the tar
jQuery是一款轻量级的JavaScript库,是最流行的客户端HTML脚本之一,它在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>
读到这里的朋友就知道你没有错过学习的机会,把这些小技巧积累起来,一定会帮助大家设计出有创意和精美的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>
读到这里的朋友就知道你没有错过学习的机会,把这些小技巧积累起来,一定会帮助大家设计出有创意和精美的Web页面。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- js仿微博实现统计字符和本地存储功能
- Bootstrap轮播加上css3动画,炫酷到底!
- 对象题目的一个坑 理解Javascript对象
- IE6-IE9使用JSON、table.innerHTML所引发的问题
- JavaScript+CSS无限极分类效果完整实现方法
- JS实现的表格操作类详解(添加,删除,排序,上移,下移)
- JS控制按钮10秒钟后可用的方法
- js实现C#的StringBuilder效果完整实例
- JavaScript判断对象是否为数组
- javascript中类的定义方式详解(四种方式)
- jquery获取select选中值的方法分析
- JS设置下拉列表框当前所选值的方法
- 点评js异步加载的4种方式
- JS模拟按钮点击功能的方法
- js实现点击复制当前文本到剪贴板功能(兼容所有浏览器)
- jquery插件jquery.confirm弹出确认消息
- js实现仿微博滚动显示信息的效果
- Javascript实现Array和String互转换的方法
- 图解Sublime Text3使用技巧