高效Web开发的10个jQuery代码片段

在过去的几年中,jQuery一直是使用最为广泛的JavaScript脚本库。今天我们将为各位Web开发者提供10个最实用的jQuery代码片段,有需要的开发者可以保存起来。

1、检测Internet Explorer版本

当涉及到CSS设计时,对开发者和设计者而言Internet Explorer一直是个问题。尽管IE6的黑暗时代已经过去,IE也越来越不流行,它始终是一个能够容易检测的好东西。当然了,下面的代码也能用于检测别的浏览器。

rush:js;"> $(document).ready(function() { if (navigator.userAgent.match(/msie/i) ){ alert('I am an old fashioned Internet Explorer'); } });

2、平稳滑动到页面顶部

这是一个最广泛使用的jQuery效果:对一个链接点击下会平稳地将页面移动到顶部。这里没什么新的内容,但是每个开发者必须要会偶尔编写一下类似函数

rush:js;"> $("a[href='#top']").click(function() { $("html,body").animate({ scrollTop: 0 },"slow"); return false; });

3、固定在顶部

非常有用的代码片段,它允许一个元素固定在顶部。对导航按钮、工具栏或重要信息框是超级有用的。

processScroll()
$win.on('scroll',processScroll)

function processScroll() {
var i,scrollTop = $win.scrollTop()

if (scrollTop >= navTop && !isFixed) {
isFixed = 1
$nav.addClass('subnav-fixed')
} else if (scrollTop <= navTop && isFixed) {
isFixed = 0
$nav.removeClass('subnav-fixed')
}
}

4、用其他内容取代html标志

jQuery使得用另外一个东西取代html标志很简单。可以利用的余地无穷无尽。

rush:js;"> $('li').replaceWith(function(){ return $("
").append($(this).contents()); });

5、检测视窗宽度

现在移动设备比过时的电脑更普遍,能够方便去检测一个更小的视窗宽度会很有帮助。幸运的是,用jQuery来做超级简单。

rush:js;"> var responsive_viewport = $(window).width();

/ if is below 481px /
if (responsive_viewport < 481) {
alert('Viewport is smaller than 481px.');
} / end smallest screen /

6、自动定位并修复损坏图片

如果你的站点比较大而且已经在线运行了好多年,你或多或少会遇到界面上某个地方有损坏的图片。这个有用的函数能够帮助检测损坏图片并用你中意的图片替换它,并会将此问题通知给访客。

rush:js;"> $('img').error(function(){ $(this).attr('src','img/broken.png'); });

7、检测复制、粘贴和剪切的操作

使用jQuery可以很容易去根据你的要求去检测复制、粘贴和剪切的操作。

rush:js;"> $("#textA").bind('copy',function() { $('span').text('copy behavIoUr detected!') }); $("#textA").bind('paste',function() { $('span').text('paste behavIoUr detected!') }); $("#textA").bind('cut',function() { $('span').text('cut behavIoUr detected!') });

8、遇到外部链接自动添加target=”blank”的属性

链接到外部站点时,你可能使用 target=”blank”的属性去在新界面中打开站点。问题在于target=”blank”属性并不是W3C有效的属性。让我们用jQuery来补 救:下面这段代码将会检测是否链接外链,如果是,会自动添加一个target=”blank”属性

rush:js;"> var root = location.protocol + '//' + location.host; $('a').not(':contains(root)').click(function(){ this.target = "_blank"; });

9、在图片上停留时逐渐增强或减弱的透明效果

一个“经典的”代码,它要放到你的工具箱里,因为你会不时地要实现它。

rush:js;"> $(document).ready(function(){ $(".thumbs img").fadeto("slow",0.6); // This sets the opacity of the thumbs to fade down to 60% when the page loads

$(".thumbs img").hover(function(){
$(this).fadeto("slow",1.0); // This should set the opacity to 100% on hover
},function(){
$(this).fadeto("slow",0.6); // This should set the opacity back to 60% on mouSEOut
});
});

10、在文本或密码输入时禁止空格键

在很多表格领域都不需要空格键,例如,电子邮件用户名密码等等等。这里是一个简单的技巧可以用于在选定输入中禁止空格键。

rush:js;"> $('input.nospace').keydown(function(e) { if (e.keyCode == 32) { return false; } });

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...