Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在onload时将div id传递给javascript?_Javascript_Html_Onload - Fatal编程技术网

如何在onload时将div id传递给javascript?

如何在onload时将div id传递给javascript?,javascript,html,onload,Javascript,Html,Onload,我有这样一个Html文件: <!doctype html> <head> <title></title> <link rel="stylesheet" type="text/css" href="style/boxClass.css" /> <script type="text/javascript"> /* lightBox class */ function box (id1,id2) { this.b

我有这样一个Html文件:

<!doctype html>


<head>
<title></title>

<link rel="stylesheet" type="text/css" href="style/boxClass.css" />
<script type="text/javascript">

/* lightBox class */

function box (id1,id2) {
    this.boxBackgroundDiv = document.getElementById (id1);
    this.boxWorkAreaDiv   = document.getElementById (id2);
}

lightBox.prototype.setBackgroundColor = function(c) {
    this.boxBackgroundDiv.style.backgroundColor = c;
    alert('Hello back');
};



function init (id1,id2)
{
   boxObj = new box (id1,id2);
   alert ("Hello");
}

</script>
</head>
<body onload="init('box1','box2')">
<div id="lightbox1" class="boxBackground">I am here</div>
<div id="lightbox2" class="boxWorkArea"><button onclick="boxObj.setBackgroundColor('Red')">I am here</button></div>

</body>
</html>

/*灯箱类*/
功能盒(id1、id2){
this.boxBackgroundDiv=document.getElementById(id1);
this.boxWorkAreaDiv=document.getElementById(id2);
}
lightBox.prototype.setBackgroundColor=函数(c){
this.boxBackgroundDiv.style.backgroundColor=c;
警报(“回复你好”);
};
函数init(id1,id2)
{
boxObj=新的盒子(id1,id2);
警惕(“你好”);
}
我在这里
我在这里
现在,当我以代码中的方式调用init函数时,它可以正常工作。但如果我通过window.onload执行以下操作,它将不起作用。在这种情况下,无法获取div id。但是我需要div id来为我的类装箱obj

<!doctype html>


<head>
<title></title>

<link rel="stylesheet" type="text/css" href="style/boxClass.css" />
<script type="text/javascript">

/* lightBox class */

function box (id1,id2) {
    this.boxBackgroundDiv = document.getElementById (id1);
    this.boxWorkAreaDiv   = document.getElementById (id2);
}

lightBox.prototype.setBackgroundColor = function(c) {
    this.boxBackgroundDiv.style.backgroundColor = c;
    alert('Hello back');
};



function init (id1,id2)
{
   boxObj = new box (id1,id2);
   alert ("Hello");
}
window.onload = init ("box1",box2);
</script>
</head>
<body>
<div id="lightbox1" class="boxBackground">I am here</div>
<div id="lightbox2" class="boxWorkArea"><button onclick="boxObj.setBackgroundColor('Red')">I am here</button></div>

</body>
</html>

/*灯箱类*/
功能盒(id1、id2){
this.boxBackgroundDiv=document.getElementById(id1);
this.boxWorkAreaDiv=document.getElementById(id2);
}
lightBox.prototype.setBackgroundColor=函数(c){
this.boxBackgroundDiv.style.backgroundColor=c;
警报(“回复你好”);
};
函数init(id1,id2)
{
boxObj=新的盒子(id1,id2);
警惕(“你好”);
}
window.onload=init(“框1”,框2);
我在这里
我在这里
两个问题:

1) 您缺少关于box2参数的引号

2) 您正在将init函数的返回值(此处为空)分配给window.onload处理程序

您应该按如下方式分配onload处理程序:

window.onload = function(){
 init ("box1","box2");
}

谢谢这起作用了。我知道这一点,但我不知道为什么我在发布这个问题之前没有尝试。