JS练习第三课
用typeof查看数据类型
-
<pre> <script type="text/javascript"> alert(typeof 12345); <span>//输出number</span> alert(typeof "abc"); <span>//输出string</span> alert(typeof document); <span>//输出object</span> </script> </pre> <script src="../js/jquery-3.2.1.min.js"></script> <script> alert("typeof 12345 -->" + typeof 12345 + '\ntypeof "abc" -->' + typeof "abc" + "\ntypeof document -->" + typeof document); </script>
View Code
用parseInt解析数字,并求和
-
<style> .d1{ width: 300px; margin: 10px auto; } input{ width: 50px; } </style> </head> <body> <div class="d1"> <input id="num1" type="text"> <span> + </span> <input id="num2" type="text"> <span> = </span> <span class="ret"> ? </span> <button>求和</button> </div> <script src="../js/jquery-3.2.1.min.js"></script> <script> $("button").on("click",function () { var num1 = parseInt($("#num1").val()); var num2 = parseInt($("#num2").val()); $(".ret").text( num1 + num2); }) </script>
View Code
版权声明:本文为Lesson-J原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。