实用jquery代码片段集合(下)

发布于,归属于jquery教程3个座位已被强势霸占! 共有402人围观    

如何隐藏除了特定选择器下的全部对象
  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.     ...
  6. </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.     ...
  5. });
将类似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);
(如果您喜欢这篇教程,可以通过支付宝打赏我们1元哦,拜谢!)

跟作者说两句

:wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :?: :-| :-x :-o :-P :-? :) :( :!: 8-O 8)

3个座位已被强势霸占!

  1. emul

    空虚的

  2. yile8

    我是刚才的逸乐。

    刚问你的那个是否有 修改时的 初始化的接口。

    另外还有在参数中: defaultLoadSelectIndex 这个的实际作用,并未很明确,可否解答。谢谢。

    明河共影回复于 2010年08月03日 8:55

    呵呵,你好。defaultLoadSelectIndex为默认加载哪个选择框的数据,(0为第一个选择框的数据)。