我无法调用prototype.js 中的函数。我哪里出错了?

发布于 2024-08-27 06:37:41 字数 850 浏览 7 评论 0原文

代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请叫√我孤独 2024-09-03 06:37:41

1:您应该使用 var 关键字声明变量 DeCheBXDeImgSayHi
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 and SayHi with the var 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 the Element 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 } after document.body.appendChild(DeImg);. It makes this part of your example code invalid.
5: In the function SayHi you look at the checked 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文