实用的 jQuery 技巧(转)

本文介绍了使用JQuery实现动画效果的方法,包括如何通过animate函数调整元素尺寸,并探讨了重复绑定click事件的问题及其解决方案。此外,还讲解了如何判断变量类型及禁用CSS样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、animate 动画

方法:$(selector).animate(styles,speed,easing,callback)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<div id="box" style="width: 100px;height: 100px;background-color: #ccc;"></div>
<script src="js/jquery-2.1.0.js"></script>
<script>
    var currHeight = 0;
    $('#box').click(function(){
        currHeight += 100;
        $(this).animate({height:currHeight,width:currHeight},function(){
            console.log('1');
        });
    });
</script>
</body>
</html>

这里写图片描述

2、JQuery给元素绑定click事件多次执行的解决方法

$("#sdfsd").on("click",function(e){ ***** }); 

  这种方法只会在原click方法中继续添加新方法;

  解决办法更改绑定方法为:

$("#sdfsd").unbind("click").click(function(e){ ***** }); 

  在绑定新click方法前对元素所绑定的click方法解绑

3.判断数据返回类型(js判断undefined类型)–typeof

 var id = $(this).attr("id").substring(0,9);

  这个看似没问题,但如果当前标签下不存在id属性, 那么 $(this).attr(“id”) 就会返回一个undefined,此时 .substring 就会抛异常,(.substring 为截取字符串,显然 undefined 不是一个有效的字符串)

  这时就要使用 typeof 判断:

var idStr = $(this).attr("id");
    //判断是否能获取到id(
    //typeof 返回的是字符串,有六种可能:"number"、"string"、
                         "boolean""object""function""undefined"if(typeof(idStr) == "undefined"){
    }else{
       var id = idStr.substring(0,9);
       $("#favoriteId").val(id); 
    }

4.禁用css样式

<script type="text/javascript">
    $(function(){
        // 禁用css样式
                // resetCss为css的文件的id,<link ... id = "resetCss">
        //document.getElementById("resetCss").disabled = true;
        $("#resetCss").attr("disabled",true);
    })
</script>

作者: 慕冬雪
链接:http://www.imooc.com/article/2505
来源:慕课网

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值