实用jquery代码片段集合

实用jquery代码片段集合

来源: 我想网  发布时间: 2010-08-03 15:38  阅读: 880 次   原文链接   全屏阅读  [收藏]  
[1] 实用jquery代码片段集合
[2] 实用jquery代码片段集合

 

如何隐藏除了特定选择器下的全部对象

 
    
$(‘#target div: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 });

选择从第一个到第X个的元素

 
    
// 从第一个到第10个
$(‘a’).slice( 0 , 10 );
// 或者
$(‘a:lt( 10 )’);

获取客户端的IP

 
    
$.getJSON(“http: // jsonip.appspot.com?callback=?”,function(data){
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 …
});
});

获取在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 ];

});

将类似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);

这个正则值得收藏,颇为经典。
向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”);

获取图片的宽高

 
    
var img = $(‘#imageid’);
var theImage = new Image();
theImage.src
= img.attr(“src”);
alert(“Width: ”
+ theImage.width);
alert(“Height: ”
+ theImage.height);

转载于:https://www.cnblogs.com/fx2008/archive/2011/12/22/2297150.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值