jsp 或 php 等view之中使用javascript简单处理的使用技巧

前端人员在jsp,php等后台mvc之的coding之时,前端人员常常需要一些少量的数据处理,直接使用js的方法无疑是开销最小的。使用的方法

使用在标签之中嵌套script标签,使用document.write()输出。

特殊写法说明

在已经显示的Html文件中,当采用调试工具的时候,edit as html的时候,在某一个标签之中直接嵌入script标签,并不能执行该js代码。

而在服务器带过来的代码,是可以使用这样的特殊方式的,如:

<span>
  <script>
    document.write(''+ fn('abc');
  </script>
</span>

这样可以使得当要处理一些jsp,php中的少量数据处理有很大的方便性。

项目中的jsp文件的设置

<div class="calc-container fl small-sceen ${info.listingStatus != 'Sold'?'onsale':'saled'}">
        <span class="icon icon-calc"></span>
        <span class="monthly-mortage">
            <%-- <c:if test="${not empty soldPrice}"> --%>
            <%-- 目前为假数据,需要后台传入保存之后的值 --%>
            <c:if test="true">
                <script>
                    (function() {
                        // 月利率
                        var l = ${mortgage.interestRate} / 1200;
                        // 总贷款利率 = (1+l)^(总月数)
                        var o = Math.pow(1 + l, ${mortgage.loanTerm} * 12);
                        // 贷款总金额
                        var e = (${info.price})*(${mortgage.interestRate}); 
                        // 每月应还银行贷款
                        var Result_PI = format_money((e * l * o / (o - 1)).toFixed(0));
                        document.write('$ '+Result_PI+'/mo');
                    })();
                </script>  
            </c:if>
        </span>
    </div>