js入門2計算平方


數據類型轉換函數

toString--轉換成字符串;--所有數據類型均可轉換成String類型

parseInt--強制轉換成整數;如果不能轉換則返回NaN

parseFloat--強制轉換成浮點數,如果不能轉換返回NaN

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3c.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3x.org/1999/xhtml">
    <head>
        <title>Javascript Day01</title>
        <meta http-equlv="content-type" content="text/html;charset=utf-8"/>
        <script language="javascript" src="js_demo1.js" type="text/javascript"></script>
        <script language="javascript" type="text/javascript">
            function firstMethod(){
                alert("Hello World in script block.");
            }
            </script>
        </head>
        <form>
            <h2>1.Hello Word</h2>
            <input type="button" value="first button" onclick="alert('Hello Word');"/>
            <input type="button" value="second button" onclick="firstMethod();"/>
            <input type="button" value="third button" onclick="secondMethod();"/>
            <h2>2.判斷數據類型,並計算平方</h2>
            <input type="text" id="txtData"/><br/>
            <input type="button" value="計算平方" onclick="getSquare()";/>
        </form>
    </body>
</html>
      js代碼:    

function secondMethod(){
    alert("hello world");
}
function getSquare(){
    var str = document.getElementById("txtData").value;
    if(isNaN(str))
        alert("請錄入數值");
    else{
        var data=parseFloat(str);
        var result = data* data;
        alert(result);
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM