• 实用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);
  • 相关阅读:
    Spark内核
    Scala(十一)泛型、项目案例
    离线数仓(一)
    SparkSparkCore(二)
    SharePoint 2013 Preview相关软件及必备组件下载地址
    SharePoint 2010 文档库AllItems.aspx页面出现乱码,打开即提示下载
    [SharePoint 2010]System.IO.FileLoadException: 找到的程序集清单定义与程序集引用不匹配
    '添加解决方案'这一部署步骤中发生错误:"未能提取解决方案中的 cab 文件"
    SharePoint 2010 BackupSPSite 备份网站集时报错 异常来自 HRESULT:0x80131904
    JSON介绍
  • 原文地址:https://www.cnblogs.com/fx2008/p/2297150.html
Copyright © 2020-2023  润新知