douzhulan1815 2012-01-05 15:14
浏览 180
已采纳

简单的PHP网页中有趣的输出

Here is a very simple PHP page with one javascript function in it. I am seeing the output differently. I am not able to understand why is it behaving that way.

In the code below, x echoes as 012012, but when I pass it into a javascript function and display it in an innerHTML, it displays differently as 5130 ??!!

Can anyone help ?

<?php
    $x= date("mY");
    echo $x;
?>
<html>
    <head>
        <script>
            function myfunc1(y)
            {
                div1 = document.getElementById("mydiv1")
                div1.innerHTML = y;
            }
        </script>
    </head>
    <body <?php echo "onload='myfunc1(".$x.")'>";?>
    <div id="mydiv1" style="background:#efefef;border:1px solid green;height:100px;width:100px;text-align:center">
    </div>
    </body>
</html>

HTML output

012012<html>
    <head>
        <script>
            function myfunc1(y)
            {
                div1 = document.getElementById("mydiv1")
                div1.innerHTML = y;
            }
        </script>
    </head>
    <body onload='myfunc1(012012)'>    <div id="mydiv1" style="background:#efefef;border:1px solid green;height:100px;width:100px;text-align:center">
    </div>
    </body>
</html>

HTML output (screenshot) Screenshot

  • 写回答

4条回答 默认 最新

  • duanming7961 2012-01-05 15:19
    关注

    This is happening because 012012 is being treated as an int. But, since it starts with 0, JavaScript treats it as base 8 (octal), and therefore converts it to 5130.

    You need to wrap 012012 in quotes, so JavaScript treats it as a string. Also, I suggest only using PHP to echo the value you need, not the entire function call. Makes it slightly easier to debug.

    <body onload="myfunc1('<?php echo $x;?>')">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?