zoukankan      html  css  js  c++  java
  • 实用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);
  • 相关阅读:
    SpringBoot:Web开发
    java @Value注解 和 @Data注解
    携程 Apollo 配置中心传统 .NET 项目集成实践
    Spring Boot 整合 JPA 使用多个数据源
    ibatis 核心原理解析!
    Spring Boot 面试,一个问题就干趴下了!
    在Docker中部署Spring Boot项目
    REDIS缓存穿透,缓存击穿,缓存雪崩原因+解决方案
    超全详解Java开发环境搭建
    SpringBoot入门(一):从HelloWorld开始
  • 原文地址:https://www.cnblogs.com/fx2008/p/2297150.html
Copyright © 2011-2022 走看看