在Tomcat中运行Javascript代码时出错

时间:2015-03-22 14:48:25

标签: javascript tomcat

我想获得th标签的元素数量。这是我的源代码:

<table class="tablehienthi" id="table">
<thead class="theadhienthi">
    <tr>
    <th class="thhienthi">Niên Khóa</th>
    <th class="thhienthi">Hệ Đào Tạo</th>
     </tr>
</thead>
<tbody class="tbodyhienthi">
    <tr class="trhienthi">  
    <td class="tdhienthi"><a style="text-decoration: none; color: rgb(30, 144, 255);" href="#">Niên Khóa 2010-2015</a></td><td class="tdhienthi">Cư Nhân</td>
    </tr>
    <tr class="trhienthi">  
    <td class="tdhienthi"><a style="text-decoration: none; color: rgb(30, 144, 255);" href="#">Niên Khóa 2010-2015</a></td><td class="tdhienthi">Kỹ Sư</td>
    </tr>
    <tr class="trhienthi">  
    <td class="tdhienthi"><a style="text-decoration: none; color: rgb(30, 144, 255);" href="#">Niên Khóa 2009-2014</a></td><td class="tdhienthi">Cư Nhân</td>
    </tr>
</tbody>

当我在浏览器中运行代码时,如果我不使用tomcat运行它,那么代码:

$(".tablehienthi tr").children().length return 8

否则如果我用tomcat运行:

$(".tablehienthi tr").children().length return 0.

我不明白?我想用tomcat运行时得到$(".tablehienthi tr").children().length。完整的源代码:

  <body>
    <table class="tablehienthi" id="table">
<thead class="theadhienthi">
    <tr>
    <th class="thhienthi">Niên Khóa</th>
    <th class="thhienthi">Hệ Đào Tạo</th>
     </tr>
</thead>
<tbody class="tbodyhienthi">
    <tr class="trhienthi">  
    <td class="tdhienthi"><a style="text-decoration: none; color: rgb(30, 144, 255);" href="#">Niên Khóa 2010-2015</a></td><td class="tdhienthi">Cư Nhân</td>
    </tr>
    <tr class="trhienthi">  
    <td class="tdhienthi"><a style="text-decoration: none; color: rgb(30, 144, 255);" href="#">Niên Khóa 2010-2015</a></td><td class="tdhienthi">Kỹ Sư</td>
    </tr>
    <tr class="trhienthi">  
    <td class="tdhienthi"><a style="text-decoration: none; color: rgb(30, 144, 255);" href="#">Niên Khóa 2009-2014</a></td><td class="tdhienthi">Cư Nhân</td>
    </tr>
</tbody>
    </table>
    <script src="js/jquery-1.9.1.js"></script>
    <script langauage="javascript">
        $('document').ready(function(){
            var temp= $(".tablehienthi th").children().length;
            alert(temp);
        });
    </script>
</body>

1 个答案:

答案 0 :(得分:0)

要计算表格行,请尝试以下操作:

 <body>
    <table class="tablehienthi" id="table">
<thead class="theadhienthi">
    <tr>
    <th class="thhienthi">Niên Khóa</th>
    <th class="thhienthi">Hệ Đào Tạo</th>
     </tr>
</thead>
<tbody class="tbodyhienthi">
    <tr class="trhienthi">  
    <td class="tdhienthi"><a style="text-decoration: none; color: rgb(30, 144, 255);" href="#">Niên Khóa 2010-2015</a></td><td class="tdhienthi">Cư Nhân</td>
    </tr>
    <tr class="trhienthi">  
    <td class="tdhienthi"><a style="text-decoration: none; color: rgb(30, 144, 255);" href="#">Niên Khóa 2010-2015</a></td><td class="tdhienthi">Kỹ Sư</td>
    </tr>
    <tr class="trhienthi">  
    <td class="tdhienthi"><a style="text-decoration: none; color: rgb(30, 144, 255);" href="#">Niên Khóa 2009-2014</a></td><td class="tdhienthi">Cư Nhân</td>
    </tr>
</tbody>
    </table>
    <script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
    <script langauage="javascript">
        $('document').ready(function(){
            var temp= $("tbody tr").length;
            alert(temp);
        });
    </script>
</body>

要计算表数据,请将javascript更改为:

<script langauage="javascript">
            $('document').ready(function(){
                var temp= $("tbody tr").children().length;
                alert(temp);
            });
        </script>