分享几个不错的jQuery小技巧 - Web前端
作者:98wpeu发布时间:2026-09-22分类:网页前端技术浏览:2
导读:JQuery是一款轻量级的javascript库,是最流行的客户端html脚本之一,它在WEB设计师和开发者中非常的有名,并且有非常多有用的插件和技术。本文我们将为大家分享一些jQ...
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>
$(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 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页面。
相关推荐
- iPhone X的出现给H5页面带来技术革新 - Web前端
- 收集一些jQuery之常用动画 - Web前端
- 实现基于jQuery立体文字渐变特效 - Web前端
- 整理一些jQuery常用特效的实现方法 - Web前端
- 谁来主导CSR(Client Side Rendering) 的SEO工作? - Web前端
- 分享JSON初学者需要掌握的一些知识点 - Web前端
- 表格中的点击更多实现滑动展开和收缩功能 - Web前端
- 【分享】Web前端开发分阶段的学习过程 - Web前端
- 响应式滚(滑)屏时,如何让元素躁动起来? - Web前端
- 收集一些Web前端开发的JS面试题【第一部分】 - Web前端
- 网页前端技术排行
-
- 1[Web前端]用javascript实现默认图片替代未显示的图片 - Web前端
- 2分析Iconfont-阿里巴巴矢量常用图标库 - Web前端
- 3jQuery实例教程:制作网页中可折叠的面板 - Web前端
- 4【第六章】Foundation之按钮和下拉功能 - Web前端
- 5基于jquery的滚动条滚动固定div(附演示下载) - Web前端
- 6jQuery编写widget的一些技巧分享 - Web前端
- 7分享精心挑选的12款优秀jQuery Ajax分页插件和教程 - Web前端
- 8jQuery实现鼠标移到元素上动态提示消息框效果 - Web前端
- 9在Mac/PC上远程调试iPhone/iPad上的网页 - Web前端
- 最近发表
-
- WordPress随机显示特色图片插件:Random Post Thumbnails
- KeePass实现Chrome浏览器自动填充密码方法一
- LNMP一键包nginx 301强制跳转到https教程
- KeePass实现Chrome浏览器自动填充密码方法二
- #建站# 免费的VPS管理软件Xshell8/Xftp8中文版下载
- 使用Xshell 8连接VPS教程_电脑登录vps的方法
- WordPress评论界面添加烟花????效果
- 不同浏览器书签同步方案:坚果云+Floccus_详细使用教程
- iOS端KeePassXC客户端APP:Strongbox Password Safe
- 给WordPress评论中的Gravatar头像图片添加ALT属性


