第一次学习jQuery出现的问题

jQuery

jQuery初印象

总是听别人说jQuery非常好用,特别实在设置样式的时候,封装了很多的方法去让别人更好的使用。在今天第一次尝试了,发现了很多问题

jQuery使用时出现的问题

在使用的时候,经常性会出现ReferenceError:$ is not defined ,可能是第一次使用,所以并不知道在引用jQuery时需要把它放在第一位。

在执行上面操作后报错依然存在,一下是我的代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>01-事件</title>
    <script src="../jquery-3.4.1.min.js" type="text/javascript" charset="utf-8" async defer></script>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        .box{
            height: 30px;
            width: 120px;
            border: 1px solid #55E6C1;
            margin: 10px;
            text-align: center;
            line-height: 30px;
            font-weight: 600;
            font-size: .8em;
        }
        #showBox{
            width: 70px;
            height: 20px;
            /* border: 1px solid black; */
            position: relative;
            left: 165px;
            top: -115px
        }
    </style>
</head>
<body>
    <div class="box" id="box1">switch</div>
    <div class="box">按钮1</div>
    <div class="box">按钮2</div>
    <div id="showBox"></div>
    <script type="text/javascript">
        $(function(){
            $("#box").click(function(){
                alert('hello world');
            })
        })
    </script>
</body>
</html>

在仔细查看时才知道,是因为async defer这个两个属性阻止代码运行,在加载网页时为了能够更快的加载JavaScript脚本,通常都会使用这个属性,但在jQuery使用时便会出现问题!!

jQuery优点

在使用时,jQuery的连续性让我有些不习惯,虽然能够方便我们使用,在由于在JavaScript使用习惯和jQuery不同,故只能慢慢适应。总的来说,jQuery非常好用,不过我认为自己估计还得在坑里呆着了,希望有人到时候能给点指点,大牛勿喷!

最后引用jQuery

Write less,do more
写得少,做得多

PS:markdown也是今天学的,勿喷~~~~

评论

暂无评论