简体   繁体   English

页面加载后调用脚本

[英]call a script after a page is loaded

I have a table which is filled from several jsp pages. 我有一个表,其中包含几个jsp页面。 I wanna alternate the table <tr> background after the page is loaded. 页面加载后,我想替换表<tr>背景。

I have to call this script: 我必须调用此脚本:

function alternate(id){ 
    var table = document.getElementById(id);   
    var rows = table.getElementsByTagName("tr");   
    for(i = 0; i < rows.length; i++){  
        if(i % 2 == 0){ 
            rows[i].style.backgroundColor = "#84939a"; 
          }else{ 
            rows[i].style.backgroundColor = "#FFFFFF";
          }      
    }
    }

How can I do that? 我怎样才能做到这一点? Thanks in advance 提前致谢

if you are using JQuery you can use: 如果您使用的是JQuery,则可以使用:

$(document).ready(function(){

.. your code

});

but doing alternating colors is much simpler with css 但是使用CSS进行颜色交替要简单得多

tr:nth-child(odd)       { background-color:#84939a; }
tr:nth-child(even)      { background-color:#FFFFFF; }
document.onload = function() {alternate(id)};

只需确保将id替换为您想用引号引起来的ID即可。

Just put your <script> just before the closing </body> tag and call it: 只需将<script>放在结束</body>标记之前并调用它:

<script>
function alternate(id){ 
var table = document.getElementById(id);   
var rows = table.getElementsByTagName("tr");   
for(i = 0; i < rows.length; i++){  
    if(i % 2 == 0){ 
        rows[i].style.backgroundColor = "#84939a"; 
      }else{ 
        rows[i].style.backgroundColor = "#FFFFFF";
      }      
}
}
alternate();
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM