(function(){
/*这里放置代码*/
})();
//为了更好的兼容性,开始前有个分号
;(function($){ //此处将$作为匿名函数的形参
/*这里放置代码,可以使用$作为jQuery的缩写别名*/
})(jQuery); //这里就将jQuery作为实参传递给匿名函数了
<label><input type="checkbox" name="foo"> Foo</label>
<label><input type="checkbox" name="bar"> Bar</label>
<button id="btn1">全选</button>
<button id="btn2">全不选</button>
<script>
jQuery.fn.extend({
check: function() {
return this.each(function() { this.checked = true; });
},
uncheck: function() {
return this.each(function() { this.checked = false; });
}
});
$('#btn1').click(function(){
$( "input[type='checkbox']" ).check();
});
$('#btn2').click(function(){
$( "input[type='checkbox']" ).uncheck();
});
</script>
var settings = {validate:false,limit:5,name:"foo"};
var options = {validate:true,name:"bar"};
var newOptions = jQuery.extend(settings,options);
console.log(newOptions);//Object {validate: true, limit: 5, name: "bar"}
jQuery.extend()方法经常被用于设置插件方法的一系列默认参数
function foo(options){
options=jQuery.extend({
name:"bar",
length:5,
dataType:"xml"
},options);
}
;(function(){
jQuery.fn.extend({
"color":function(value){
if(value == undefined){
return this.css("color");
}else{
return this.css("color",value);
}
}
});
})(jQuery);
;(function(){
jQuery.fn.extend({
"color":function(value){
return this.css("color",value);
}
});
})(jQuery);
<span id="test">测试文字</span>
<input type="color" id="color">
<script>
;(function($){
$.fn.extend({
"color":function(value){
return this.css("color",value);
}
});
})(jQuery);
$('#color').on('change',function(){
$('#test').color($(this).val());
})
</script>
;(function(){
jQuery.fn.extend({
"color":function(value){
//插件代码
},
"border":function(value){
//插件代码
},
"background":function(value){
//插件代码
}
});
})(jQuery);
;(function($){
$.extend({
ltrim:function( text ){
return (text || "").replace(/^\s+/g,"");
},
rtrim:function( text ){
return (text || "").replace(/\s+$/g,"");
}
});
})(jQuery);
var $str = " test ";
console.log($.trim($str));//'test'
console.log($.ltrim($str));//'test '
console.log($.rtrim($str));//' test'
function (a,i,m){
//...
}
;(function($){
$.extend($.expr[":"],{
between:function(a,i,m){
var temp=m[3].split(",");
return +temp[0]<i&&i<+temp[1];
}
});
})(jQuery);
;(function($){
var $index = -1;
$.extend($.expr[":"],{
between:function(a,i,m){
var temp=m[3].split(",");
$index++;
return +temp[0]<$index&&$index<+temp[1];
}
});
})(jQuery);
<div>
<i>0</i>
<i>1</i>
<i>2</i>
<i>3</i>
<i>4</i>
<i>5</i>
</div>
<button id="btn">测试</button>
<script>
;(function($){
var $index = -1;
$.extend($.expr[":"],{
between:function(a,i,m){
var temp=m[3].split(",");
$index++;
return +temp[0]<$index&&$index<+temp[1];
}
});
})(jQuery);
$('#btn').click(function(){
$('i:between(1,5)').css('background','lightblue');
});
</script>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有