对于动态加载到页面的html元素,每次都需要重新绑定事件到这些元素
插件livequery,后来通过javascript添加的元素都会被绑定到事件
普通方法绑定单击事件
$(“a”).click(function(){
//doSomething
})
但是这样通过js动态加载的内容中的超链接元素则不会绑定单击事件
$(‘a‘).livequery(‘click’,function(){
//doSomething
})
$(document).ready(function(){
$('li').livequery(function(){
//hover函数绑定over,out事件
$(this).hover(function()
{$(this).addClass('hover');
},function(){
$(this).removeClass('hover');
});
},function(){
//解除绑定
$(this).unbind('mouseover')
.unbind('mouseout');
});
});
//加延迟防止单击失效
<script type="text/javascript"> $(function(){ $('#myList').sortable({delay:1}); }); </script>//cookie
<script type="text/javascript"> $(function(){ var COOKIE_NAME='username'; if($.cookie(COOKIE_NAME)){ $('#username').val($.cookie(COOKIE_NAME)); }$(#check).click(function(){
if(this.checked){ $.cookie(COOKIE_NAME,$('#username').val(),{path:'/',expires:10}); }else{ $.cookie(COOKIE_NAME,null,{path:'/'}); } }); });</script>
/*封装对象方法的插件 jQuery.fn.extend() 封装全局函数的插件 jQuery.extend() 选择器插件 jQuery.extend() jquery.[插件名].js 所有对象的方法都应附加到jQuery.fn对象上 利用闭包的特性,既可以避免内部临时变量影响全局空间,又可以在插件内部继续使用$作为jquery的别名 */ //()作为参数传递进去,供内部使用 (function() {})();; (function($) {var foo;
var bar = function() {}
$.BAR = bar;})(jQuery);
; (function($) { //此处编写jquery插件代码})(jQuery);
/* jQuery提供了两个用于扩展jquery的功能jQuery.fn.extend() jQuery.extend() jQuery.extend()用一个或多个其他对象来扩展一个对象,然后返回被扩展的对象 */ var settings={validate:false,limit:5,name:"foo"}; var options={validate:false,name:"bar"}; var newOptions=jQuery.extend(settings,options); console.log(newOptions); jquery.color.js ;(function() { jQuery.fn.extend({ "color": function(value) { return this.css('color', value); }, "border":function(value){},
"background":function(value){}
});
//隔行变色;(function($){
$.fn.extend({ "alterBgColor":function(options){ //设置默认值 options=$.extend({ odd:"odd", even:"even", selected:"selected" },options); $('tbody>tr:odd',this).addClass(options.odd); $('tbody>tr:even',this).addClass(options.even); $('tbody>tr',this).click(function(){ var hasSelected=$(this).hasClass(options.selected); $(this)[hasSelected?"removeClass":"addClass"](options.selected).find(':checkbox').attr('checked',!hasSelected); }); $('tbody>tr:has(:checked)',this).addClass(options.selected); return this;}
}) })//封装全局函数的插件,在jquery命名空间添加了一个函数 ;(function($) { $.extend({ ltrim: function(text){ return (text || "").replace( /^\s+/g,""); }, rtrim:function(text){ return (text ||"").replace( /\s+$/g,""); } }); })(jQuery);
//jquery提供了一套方法让用户制作插件来使用自定义选择器
//between ;(function($){ $.$.extend(jQuery.expr[":"], { between:function(a,i,m){ var tmp=m[3].split(","); return tmp[0]-0<i&&i<tmp[1]-0; } }); })(jQuery); html放到http://validator.w3.org/