Javascript函数调用页面onload和onclick?

sto*_*low 1 javascript

我需要帮助更改页面加载时调用的函数,以及用户点击带有ID的某个div后的任何时间.到目前为止我有这个:

window.onload=function() {
//Do your stuff, JS!
}
Run Code Online (Sandbox Code Playgroud)

是的,我几乎不知道任何JS ......

编辑:我将包括我的JS功能(我没有自己做,明显是另一个更好,更聪明的人:P)

    function() {
    var maxHeight = 0;
    //get the column containers
    var colsA = document.getElementById("Content").childNodes;

    //get the height of the tallest column
    for(var i=0; i < colsA.length; i=i+1) {
         if(colsA[i].clientHeight > maxHeight) maxHeight = colsA[i].clientHeight;
    }

 //set all the column containers heights to maxHeight
    for(var i=0; i < colsA.length; i=i+1) {
         if(colsA[i].nodeType == 1) colsA[i].style.height = maxHeight+'px';
    }
} 
Run Code Online (Sandbox Code Playgroud)

它的作用:我有一个包含x个列div的div容器.由于内容的原因,这些列的高度不同.此功能使所有div高度相同.

当我的页面加载时,此代码运行完美.但事后却没有.我有一些可折叠的div可以存储额外的信息,当用户点击它时会进一步提升高度.这就是为什么我想到了那个id的onclick ...不幸的是id是由php动态生成的.

Sim*_*mon 7

您可以单独定义一个函数,然后将其绑定到多个事件处理程序:

function myHandler(){...}
window.onload = myHandler;
myElement.onclick= myHandler;
...
Run Code Online (Sandbox Code Playgroud)