一個javascript的Math對象的方法實現,將字符串,轉換成實際函數

使用前請導入jquery-2.2.2.js

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <script src="../jquery/jquery-2.2.2.js" type="text/javascript" charset="utf-8"></script>
        <script type="text/javascript">
            var mathArraySome = new Array();
            mathArraySome[0] = "選擇函數:描述函數";
            mathArraySome[1] = "round(x):把數四捨五入爲最接近的整數。";
            mathArraySome[2] = "random():返回 0 ~ 1 之間的隨機數。";
            mathArraySome[3] = "max(x,y):返回 x 和 y 中的最高值。";
            mathArraySome[4] = "min(x,y):返回 x 和 y 中的最低值。";
            mathArraySome[5] = "abs(x):返回數的絕對值。";
            mathArraySome[6] = "acos(x):返回數的反餘弦值。";
            mathArraySome[7] = "asin(x):返回數的反正弦值";
            mathArraySome[8] = "atan(x):以介於 -PI/2 與 PI/2 弧度之間的數值來返回 x 的反正切值。";
            mathArraySome[9] = "atan2(y,x):返回從 x 軸到點 (x,y) 的角度(介於 -PI/2 與 PI/2 弧度之間)。";
            mathArraySome[10] = "ceil(x):對數進行上舍入。";
            mathArraySome[11] = "cos(x):返回數的餘弦。";
            mathArraySome[12] = "exp(x):返回 e 的指數。";
            mathArraySome[13] = "floor(x):對數進行下舍入。";
            mathArraySome[14] = "log(x):返回數的自然對數(底爲e)。";
            mathArraySome[15] = "pow(x,y):返回 x 的 y 次冪。";
            mathArraySome[16] = "sin(x):返回數的正弦。";
            mathArraySome[17] = "sqrt(x):返回數的平方根";
            mathArraySome[18] = "tan(x):返回角的正切。";
            mathArraySome[19] = "toSource():返回該對象的源代碼。";
            mathArraySome[20] = "valueOf():返回 Math 對象的原始值。";
            $(function(e){
                $("#func").change(function(){
                var $func =	$(this).val();
                alert(mathArraySome[$func].substring(mathArraySome[$func].indexOf(":")+1,mathArraySome[$func].length));
                $("#funsome").text(mathArraySome[$func].substring(mathArraySome[$func].indexOf(":")+1,mathArraySome[$func].length));
                });
                $("#pare").blur(function(){
                    var $func =	$("#func").val();
                    var $pare = $("#pare").val();
                if($func != 0 && $pare.length > 0) {
                    var $hanshu = "Math." + mathArraySome[$func].substring(0, mathArraySome[$func].indexOf("(") + 1) + $pare + ")"; //得到要實現的數學函數
                    //關鍵代碼,在寫入script的腳本然後再講字符串表示的代碼寫在span中
                    ***$("#result").html("<script> $('#result').text("+$hanshu+")</"+"script>");***
                } else {
                    alert("請選擇正確的函數!");
                }
                })

            });
        </script>
        <title></title>
    </head>

    <body>

        <p>如果需要多哥參數的函數需要用‘,’(逗號)分割</p>
        <table width="70%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td><label for="func"></label>
                    <script type="application/javascript">
                        //遍歷循環數組mathArraySome的到下拉列表的值
                        document.write("<select name='func' id='func'>");
                        for(var i = 0; i < mathArraySome.length; i++) {
                            //將鍵和值分開顯示鍵:即方法
                            document.write();
                            document.write("<option value='" + i + "'>" + mathArraySome[i].substring(0, mathArraySome[i].indexOf(":")) + "</option>");
                        }
                        document.write("</select>");
                    </script>
                    <span id="funsome">#####</span></td>
                <td><label for="textfield"></label>
                    <input type="text" name="para" id="pare" onblur="doMathArray();"></td>
                <td>==&gt;</td>
                <td><span id="result">#####</span></td>
            </tr>
        </table>
    </body>
</html>

效果圖:

math.function

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章