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

jquery实用代码片段集合_jquery

2025/12/26 23:31:02发布16次查看
加载google的jquery库
script type=text/javascript src=http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js>
script>
有利于加快加载速度(已经得到验证)。
修改图片src更新图片
$(imageobj).attr('src', $(imageobj).attr('src') + '?' + math.random() );

这是很实用的技巧,曾经有人问明河,为什么他已经修改了图片的src,但图片没变化呢?原因在于缓存,给图片路径后加个随机数参数即可。
加载多张图片,判断加载完成状态
var totalimages = 10;var loadedimages = 0;$(img/>).load(function() {++loadedimages;if(loadedimages == totalimages){//全部图片加载完成时….. } });

双击不选中文本
var clearselection = function () {if(document.selection && document.selection.empty) { document.selection.empty(); } else if(window.getselection) {var sel = window.getselection(); sel.removeallranges(); } } $(element).bind('dblclick',function(event){ clearselection(); });

对一个列表进行排序
ul>li>cloud/li> li>sun/li> li>rain/li> li>snow/li> /ulvar items = $('.to_order li').get(); items.sort(function(a,b){var keya = $(a).text();var keyb = $(b).text();if (keya keyb) return -1;if (keya > keyb) return 1;return 0; });var ul = $('.to_order'); $.each(items, function(i, li){ ul.append(li); });

(中文无效)绑定右击事件
$(document).ready(function(){ $(document).bind(contextmenu,function(e){return false; }); });

扩展jquery的对象选择器
$.extend($.expr[':'], {//选择器名 morethanathousand : function (a){return parseint($(a).html()) > 1000; } }); $(document).ready(function() { $('td:morethanathousand').css('background-color', '#ff0000′); });

检查对象是否存在
if ($(#elementid).length) {//……}

取消一个ajax请求
var req = $.ajax({type: post,url: someurl,data: id=1″,success: function(){}});//取消ajax请求req.abort()

检查cookies是否可用
$(document).ready(function() {var dt = new date();dt.setseconds(dt.getseconds() + 60);document.cookie = cookietest=1; expires= + dt.togmtstring();var cookiesenabled = document.cookie.indexof(cookietest=) != -1;if(!cookiesenabled){//cookies不能用……..}});

获取当前元素在元素集内的索引值
$(ul > li).click(function () {var index = $(this).prevall().length; });//如果用的是jquery1.4,明河推荐使用以下方法: $(ul > li).click(function () {var index = $(this).index();

该用户其它信息

VIP推荐

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