如何隐藏除了特定选择器下的全部对象
  1. $(‘#target div:not(#exclude)’).hide();
  2. //或者
  3. $(‘#target’).children().filter(‘:not(#exclude)’).hide();

filter()起到过滤的作用。

寻找带有指定字符串的元素

  1. var foundin = $(‘*:contains(” 明河”)’);
获取垂直滚动距离
  1. alert($(document).scrollTop());

scrollTop()非常实用的一个方法。

向表格追加一行数据
  1. $(‘#myTable tr:last’).after(‘<tr>…</tr>’);
超过一个属性时的过滤
  1. var elements = $(‘#someid input[type=sometype][value=somevalue]‘).get();
让cookies在X分钟后过期
  1. var date = new Date();
  2. date.setTime(date.getTime() + (x * 60 * 1000));
  3. $.cookie(‘example’, ‘foo’, { expires: date });
选择从第一个到第X个的元素
  1. //从第一个到第10个
  2. $(‘a’).slice(0,10);
  3. //或者
  4. $(‘a:lt(10)’);
获取客户端的IP
  1. $.getJSON(“http://jsonip.appspot.com?callback=?”,function(data){
  2. alert( “你的IP:” + data.ip);
  3. });

这是利用了jsonip.appspot.com提供的取IP服务。

解析XML数据源
  1. <?xml version=”1.0″ ?>
  2. <result>
  3. <item>
  4. <id>1</id>
  5. <title>title1</title>
  6. <description>desc1</description>
  7. </item>
  8. <item>
  9. <id>2</id>
  10. <title>title2</title>
  11. <description>desc2</description>
  12. </item>
  13. <!– … –>
  14. </result>
  1. $.get(‘file.xml’,{},function(data){
  2. $(‘item’,data).each(function(){
  3. var $this&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = $(this);
  4. var id &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;= $this.find(‘id’).text();
  5. var title &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;= $this.find(‘title’).text();
  6. var description = $this.find(‘description’).text();
  7. //do something …
  8. });
  9. });
获取在id中的数字
  1. <div id=”sites”>
  2. <a id=”site_1″ href=”http://siteA.com”>siteA</a>
  3. <a id=”site_2″ href=”http://siteB.com”>siteB</a>
  4. <a id=”site_3″ href=”http://siteB.com”>siteC</a>
  5. </div>
  1. $(“#sites a”).click(function(){
  2. var $this &nbsp;&nbsp; &nbsp;= $(this);
  3. var nmb &nbsp;&nbsp; &nbsp;= $this.attr(‘id’).match(/site_(\d+)/)[1];
  4. });
将类似12343778 转成 12.343.778的形式
  1. var delimiter = ‘.’;
  2. $(‘#result’).html()
  3. .toString()
  4. .replace(new RegExp(“(^\\d{“+($this.html().toString().length%3||-1)+”})(?=\\d{3})”),”$1″ + delimiter)
  5. .replace(/(\d{3})(?=\d)/g,”$1″ + delimiter);

这个正则值得收藏,颇为经典。

向firebug的控制面板发送消息
  1. jQuery.fn.log = function (msg) {
  2. console.log(“%s: %o”, msg, this);
  3. return this;
  4. };
  5. $(‘#some_div’).find(‘li.source > input:checkbox’).log(“sources to uncheck”).removeAttr(“checked”);
获取图片的宽高
  1. var img = $(‘#imageid’);
  2. var theImage = new Image();
  3. theImage.src = img.attr(“src”);
  4. alert(“Width: ” + theImage.width);
  5. alert(“Height: ” + theImage.height);

您可能会感兴趣的其他文章

转载请标注编辑来源:实用jquery代码片段集合[下]

查看本文来源   我想网 板凳 编辑

Tags: ,

分享
QQ书签
百度搜藏
Del.icio.us
Google书签
和讯网摘
天极网摘

留言