码迷,mamicode.com
首页 > Web开发 > 详细

js坑 把数字型的字符串默认为数字 把前面的0给去掉了("001")

时间:2018-04-16 20:34:03      阅读:905      评论:0      收藏:0      [点我收藏+]

标签:需要   ref   log   script   影响   ons   UNC   history   pre   

<script>
    b("001");
    function b(id)
    {
        console.log("b函数的id:"+id);
        //var history = "<a href=‘#‘ onclick=‘a(" +id + ")‘>历史</a>"; 错误代码这里id默认理解成数字类型,需要"",直接在前面添加会影响html结构报语法错误所以需要把"需要转义\"
        var history = "<a href=‘#‘ onclick=‘a(\"" +id + "\")‘>历史</a>";
        document.write(history)
    }
    function a(id)
    {
        console.log("a函数的id:"+id);
    }
</script>

错误代码返回结果:

b函数的id:001
a函数的id:1

正确代码返回结果:

b函数的id:001
a函数的id:001

js坑 把数字型的字符串默认为数字 把前面的0给去掉了("001")

标签:需要   ref   log   script   影响   ons   UNC   history   pre   

原文地址:https://www.cnblogs.com/sdzj/p/8858148.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!