您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

18个非常棒的jQuery代码片段_jquery

2025/10/1 14:20:03发布11次查看
1、jquery实现的内链接平滑滚动
不需要使用太复杂的插件,只要使用下载这段代码即可实现基于内部链接的平滑滚动
$('a[href^=#]').bind('click.smoothscroll',function (e) {e.preventdefault(); var anchor = this.hash,$target = $(target); $('html, body').stop().animate({'scrolltop': $target.offset().top}, 500, 'swing', function () {window.location.hash = anchor;}); });
2、使用jquery获取所有节点
var $element = $('#gbtags'); var $nodes = $element.contents(); $nodes.each(function() { if(this.nodetype === 3 && $.trim($(this).text())) { $(this).wrap(''); }});
3、限制选择框选择个数
$(#categories option).click(function(e){ if ($(this).parent().val().length maxheight){ ratio = maxheight / height; $(this).css(height, maxheight); $(this).css(width, width * ratio); width = width * ratio; } }); //$(#contentpage img).show(); // image resize});
12、鼠标滑动时的渐入和渐出
$(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 });});
13、制作等高的列
var max_height = 0;$(div.col).each(function(){ if ($(this).height() > max_height) { max_height = $(this).height(); }});$(div.col).height(max_height);
14、图片预加载
(function($) { var cache = []; // arguments are image paths relative to the current page. $.preloadimages = function() { var args_len = arguments.length; for (var i = args_len; i--;) { var cacheimage = document.createelement('img'); cacheimage.src = arguments[i]; cache.push(cacheimage); } } jquery.preloadimages(image1.gif, /path/to/image2.png);
15、获取 url 中传递的参数
$.urlparam = function(name){ var results = new regexp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href); if (!results) { return 0; } return results[1] || 0;}
16、禁用表单的回车键提交
$(#form).keypress(function(e) { if (e.which == 13) { return false; }});
17、让整个div可以被点击
www.jb51.net $(.mybox).click(function(){ window.location=$(this).find(a).attr(href); return false;});
18、在新窗口打开链接 (target=”blank”)
$('a[@rel$='external']').click(function(){ this.target = _blank;});
大家可以结合之前小编整理的文章进行学习,把实用的jquery代码片段进行汇总,以便日后学习使用。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product