一个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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值