问题解决系列

85 阅读2分钟

哈喽哈喽,这里是小菜不拖延博主

打卡day7~

问题

image.png

结果:

image.png 在最后显示计算结果时出现问题

解决:发现问题是没有整体加括号,程序按照顺序进行计算,就为——字符串+数字-数字,所以出现NaN.这里其实不用-‘0’也可以,因为2022-getyear已经发生了隐式转化。所以发现,不是固定形式“字符串-数字”才可以转化为数字型,只要是字符串和数字发生减、乘、除都可以

2.常出现问题:忘记prompt中得到的是字符串类型,直接进行数字计算,要注意。

问题:显示num2not defined

image.png

解决:一定要先调用函数,此时num2才不会出问题

问题:想要实现密码登录,password和text的变换效果.但是点击之后没有发生变化

image.png

解决:出现了两个id,所以当我们使用psw出现问题。把这个多余的id删掉即可

image.png

问题:想要实现点击之后变换颜色。但是点击之后没有反应

image.png

解决:问题出在元素获取方法混淆,getElementById是通过id进行获取,而这里我却填的是标签名。解决办法:要么给div加上id,要么把getElementById改成querySelect。

问题:点击之后按钮没有改变颜色

image.png

解决:改成this

image.png

问题:无法显示文字

<style>
    .box{
        width: 200px;
        height: 200px;
        background-color: aquamarine;
        margin: auto;
    }
</style>
<body>
    <div class="box"></div>
    <script>
        var box=document.querySelector('.box');
       
       box.mousemove=function(e)
       {
        var x=e.pageX-this.offsetLeft;
        var y=e.pageY-this.offsetTop;
        this.innerHTML='x坐标是'+x+'y的坐标是'+y;
       }
 
    </script>
</body>

解决:①,mousemove前面加上on

②采用另外一种函数书写方式,mousemove不用加on

     box.addEventListener('mousemove',function(e){
            var x=e.pageX-this.offsetLeft;
        var y=e.pageY-this.offsetTop;
        this.innerHTML='x坐标是'+x+'y的坐标是'+y;
        })

问题:无法实现动画缓动效果

       var two=document.querySelector('.two');
            function animate1(obj,target)
            {
                var step=(target-two.offsetLeft)
                var timer=setInterval(function(){
            if(two.offsetLeft>=target)
            {
                clearInterval(timer);
            }
           two.style.left=two.offsetLeft+step+'px';
           }, 30);
            }
           animate1(two,300);

解决:要把step的计算放到定时器中,才能实现效果.也要注意设置动画的时间,时间不合适会发现观察不到动画效果。

function animate(obj,target)
        {
          
            var timer=setInterval(function(){
                var step=(target-one.offsetLeft)/10
        if(one.offsetLeft>=400)
        {
            clearInterval(timer);
        }
        one.style.left=one.offsetLeft+step+'px';
       }, 100);
        }
       animate(one,300);