我无法调用prototype.js 中的函数。我哪里出错了?
代码:
DeCheBX = $('MyDiv').insert(new Element('input', { 'type': 'checkbox', 'id': "Img" + obj[i].Nam, 'value': obj[i].IM, 'onClick': SayHi(id) }));
document.body.appendChild(DeCheBX);
DeImg = $('MyDiv').insert(new Element('img', { 'id': "Imgx" + obj[i].Nam, 'src': obj[i].IM }));
document.body.appendChild(DeImg);
}
SayHi = function(x) {
try {
if($('x').checked == true)
{
alert("press" + x);
}
}
catch (e) {
alert("error");
}
};
Code:
DeCheBX = $('MyDiv').insert(new Element('input', { 'type': 'checkbox', 'id': "Img" + obj[i].Nam, 'value': obj[i].IM, 'onClick': SayHi(id) }));
document.body.appendChild(DeCheBX);
DeImg = $('MyDiv').insert(new Element('img', { 'id': "Imgx" + obj[i].Nam, 'src': obj[i].IM }));
document.body.appendChild(DeImg);
}
SayHi = function(x) {
try {
if($('x').checked == true)
{
alert("press" + x);
}
}
catch (e) {
alert("error");
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1:您应该使用
var
关键字声明变量DeCheBX
、DeImg
和SayHi
。2:在示例中,不清楚
obj[i]
来自何处以及它是什么。错误可能源于它不是您所希望的。3:您肯定不想调用函数
SayHi
并将其返回值(未定义)分配给您构造并插入到的
。你做事的方式,最好是像Element
的 onclick 处理程序>'MyDiv'"SayHi(this);"
这样的字符串。4:
document.body.appendChild(DeImg);
后面多了一个}
。它使示例代码的这一部分无效。5:在函数
SayHi
中,您查看 id 为“x”的元素的checked
属性。在我看来,您宁愿使用传递给函数的 x 值(参见上面的 3)。1: You should declare your variables
DeCheBX
,DeImg
andSayHi
with thevar
keyword.2: In the example it is unclear where
obj[i]
comes from and what it is. Errors may stem from it being not what you hope.3: You most certanly don't want to call the function
SayHi
and assign its return value (undefined) to the onclick handler of theElement
you construct and insert into'MyDiv'
. The way you are doing things, It'd better be a string like"SayHi(this);"
.4: There is an extra
}
afterdocument.body.appendChild(DeImg);
. It makes this part of your example code invalid.5: In the function
SayHi
you look at thechecked
property of an element with id 'x'. It seems to me that you'd rather want to use the value of the x passed to the function (see 3 above).