在Tomcat中运行Javascript代码时出错

Errors when running Javascript code in Tomcat

本文关键字:代码 出错 Javascript 运行 Tomcat      更新时间:2023-09-26

我想要得到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>

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

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