javascript - 这个判断为什么不生效呢if($(".a").css("width") == 100){}

查看:140
本文介绍了javascript - 这个判断为什么不生效呢if($(".a").css("width") == 100){}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

为什么这个判断不生效呢?我是想判断这个$(".a") 宽度是否为100px,是不是哪里写错了?

$(function(){
    $(".a").animate({width:"100px"},3000);
    if($(".a").css("width") == 100){
        console.log("100");
    }
})

我刚刚解决了····  animate(style,speed,callback),写在animate里面就可以

解决方案

首先 JavaScript 是异步加载,你那动画是3秒之后才加载的,所以宽度不会是100px,你只要加个定时器就可以了; 第二 你比较的时候少了单位 ‘px’, 修改后的代码如下

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <script src="jquery-1.9.1.min.js"></script>
    <title>Document</title>
</head>

<body>
    <div class="a">
    </div>
</body>

</html>
<script>
$(function() {
    $(".a").animate({
        width: "100px"
    }, 3000);
    // console.log($(".a").css("width"))

    setInterval(function() {
        if ($(".a").css("width") == '100px') {
            console.log("100");
        }
    }, 3000)

})
</script>

这篇关于javascript - 这个判断为什么不生效呢if($(&quot;.a&quot;).css(&quot;width&quot;) == 100){}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆