实用jquery代码片段集合
如何隐藏除了特定选择器下的全部对象
$(‘#target div:not(#exclude)’).hide();
// 或者
$(‘#target’).children().filter(‘:not(#exclude)’).hide();
// 或者
$(‘#target’).children().filter(‘:not(#exclude)’).hide();
filter()起到过滤的作用。
寻找带有指定字符串的元素
var
foundin
=
$(‘
*
:contains(” 明河”)’);
获取垂直滚动距离
alert($(document).scrollTop());
scrollTop()非常实用的一个方法。
向表格追加一行数据
$(‘#myTable tr:last’).after(‘
<
tr
>
…
<
/
tr>’);
超过一个属性时的过滤
var
elements
=
$(‘#someid input[type
=
sometype][value
=
somevalue]‘).get();
让cookies在X分钟后过期
var
date
=
new
Date();
date.setTime(date.getTime() + (x * 60 * 1000 ));
$.cookie(‘example’, ‘foo’, { expires: date });
date.setTime(date.getTime() + (x * 60 * 1000 ));
$.cookie(‘example’, ‘foo’, { expires: date });
选择从第一个到第X个的元素
//
从第一个到第10个
$(‘a’).slice( 0 , 10 );
// 或者
$(‘a:lt( 10 )’);
$(‘a’).slice( 0 , 10 );
// 或者
$(‘a:lt( 10 )’);
获取客户端的IP
$.getJSON(“http:
//
jsonip.appspot.com?callback=?”,function(data){
alert( “你的IP:” + data.ip);
});
alert( “你的IP:” + data.ip);
});
这是利用了jsonip.appspot.com提供的取IP服务。
解析XML数据源
<?
xml version
=
”
1.0
″
?>
< result >
< item >
< id > 1 < / id>
< title > title1 < / title>
< description > desc1 < / description>
< / item>
< item >
< id > 2 < / id>
< title > title2 < / title>
< description > desc2 < / description>
< / item>
<! – … – >
< / result>
$.get(‘file.xml’,{}, function (data){
$(‘item’,data).each( function (){
var $ this & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; = $( this );
var id & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; = $ this .find(‘id’).text();
var title & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; = $ this .find(‘title’).text();
var description = $ this .find(‘description’).text();
// do something …
});
});
< result >
< item >
< id > 1 < / id>
< title > title1 < / title>
< description > desc1 < / description>
< / item>
< item >
< id > 2 < / id>
< title > title2 < / title>
< description > desc2 < / description>
< / item>
<! – … – >
< / result>
$.get(‘file.xml’,{}, function (data){
$(‘item’,data).each( function (){
var $ this & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; = $( this );
var id & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; = $ this .find(‘id’).text();
var title & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; = $ this .find(‘title’).text();
var description = $ this .find(‘description’).text();
// do something …
});
});
获取在id中的数字
<
div id
=
”sites”
>
< a id = ”site_1″ href = ”http: // siteA.com”>siteA</a>
< a id = ”site_2″ href = ”http: // siteB.com”>siteB</a>
< a id = ”site_3″ href = ”http: // siteB.com”>siteC</a>
…
< / div>
$(“#sites a”).click( function (){
var $ this & nbsp; & nbsp; & nbsp; = $( this );
var nmb & nbsp; & nbsp; & nbsp; = $ this .attr(‘id’).match( / site_(\d+) / )[ 1 ];
…
});
< a id = ”site_1″ href = ”http: // siteA.com”>siteA</a>
< a id = ”site_2″ href = ”http: // siteB.com”>siteB</a>
< a id = ”site_3″ href = ”http: // siteB.com”>siteC</a>
…
< / div>
$(“#sites a”).click( function (){
var $ this & nbsp; & nbsp; & nbsp; = $( this );
var nmb & nbsp; & nbsp; & nbsp; = $ this .attr(‘id’).match( / site_(\d+) / )[ 1 ];
…
});
将类似12343778 转成 12.343.778的形式
var
delimiter
=
‘.’;
$(‘#result’).html()
.toString()
.replace( new RegExp(“( ^ \\d{“ + ($ this .html().toString().length % 3 ||- 1 ) + ”})( ?= \\d{ 3 })”),”$ 1 ″ + delimiter)
.replace( / (\d{3})(?=\d) / g,”$ 1 ″ + delimiter);
$(‘#result’).html()
.toString()
.replace( new RegExp(“( ^ \\d{“ + ($ this .html().toString().length % 3 ||- 1 ) + ”})( ?= \\d{ 3 })”),”$ 1 ″ + delimiter)
.replace( / (\d{3})(?=\d) / g,”$ 1 ″ + delimiter);
这个正则值得收藏,颇为经典。
向firebug的控制面板发送消息
jQuery.fn.log
=
function
(msg) {
console.log(“ % s: % o”, msg, this );
return this ;
};
$(‘#some_div’).find(‘li.source > input:checkbox’).log(“sources to uncheck”).removeAttr(“checked”);
console.log(“ % s: % o”, msg, this );
return this ;
};
$(‘#some_div’).find(‘li.source > input:checkbox’).log(“sources to uncheck”).removeAttr(“checked”);
获取图片的宽高
var
img
=
$(‘#imageid’);
var theImage = new Image();
theImage.src = img.attr(“src”);
alert(“Width: ” + theImage.width);
alert(“Height: ” + theImage.height);
var theImage = new Image();
theImage.src = img.attr(“src”);
alert(“Width: ” + theImage.width);
alert(“Height: ” + theImage.height);