为什么onclick的处理函数没有按预期工作?
我有以下一小段代码:
var instance = this;
window.onload = function () {
for (var i = 0; i < array.length; ++i) {
var currentDivId= array[i];
var currentDiv = document.getElementById(currentDivId);
try {
if (!currentDiv) {
throw 'Div id not found: ' + currentDivId;
}
var image = document.createElement('img');
image.src = 'img.jpg';
image.onclick = function() {
instance.doSomething(currentDivId);
};
currentDiv.appendChild(image);
}
catch(e) {
console.warn('oops');
}
}
};
这段代码传递了一个 div 的 id 数组。它的作用是在每个 div 上渲染图像并设置它们的 onclick 属性。
假设我有一个字符串数组: ['abc', 'xyz']
我希望代码将图像放置在 中;
和 内的另一个图像。
当您单击第一张图像时,应使用参数 'abc'
调用 instance.doSomething
函数,反之亦然。
但代码没有按预期工作。它始终使用数组中的最后一个参数调用 instance.doSomething
,在本例中为 'xyz'
。
我是 JS 的新手,仍然没有掌握其内部工作原理。这里出了什么问题,我该如何修复它?
任何帮助表示赞赏。
I have the following little piece of code:
var instance = this;
window.onload = function () {
for (var i = 0; i < array.length; ++i) {
var currentDivId= array[i];
var currentDiv = document.getElementById(currentDivId);
try {
if (!currentDiv) {
throw 'Div id not found: ' + currentDivId;
}
var image = document.createElement('img');
image.src = 'img.jpg';
image.onclick = function() {
instance.doSomething(currentDivId);
};
currentDiv.appendChild(image);
}
catch(e) {
console.warn('oops');
}
}
};
This code is passed an array of id of divs. What it does is that, it renders an image at each of those divs and set their onclick
property.
Say I have an array of strings: ['abc', 'xyz']
I want the code to place an image inside <div id="abc"></div>
and another image inside <div id="xyz"></div>
.
When you click the first image, instance.doSomething
function should be called with parameter 'abc'
and vice versa.
But the code does not work as expected. It always calls instance.doSomething
with the last parameter in the array, in this case, 'xyz'
.
I'm new to JS and still don't have a solid grasp of its inner workings. What's wrong here and how can I fix it?
Any help appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
应该可以做到这一点。由于我们知道图像位于我们想要获取的 div 内,因此只需向上移动一个 dom 元素即可获取其 id。
That should do it. Since we know that the image is inside the div we want to get at, just go one dom element up and get its id.
欢迎来到 Javascript 范围问题的奇妙世界。就目前而言,JS 将您的 onclick 代码视为“当单击此对象时,在单击发生时获取存储在 currentDivID 变量中的值并将其传递给 doSomething 函数”。
您应该做的是将参数基于图像对象本身。每个 DOM 对象都知道它在 DOM 树中的位置,因此在单击它时,onclick 代码应该使用 DOM 遍历操作来找出它所在的 div 并动态检索其 ID。这样您就不必担心绑定变量和范围问题...只需找出哪个 div 包含您的图像并在运行时获取 ID 即可。
Welcome to the wonderful world of Javascript scoping issues. As it stands now, JS is treating your onclick code as something like "when this object is clicked, fetch the value stored in the currentDivID variable AT THE TIME THE CLICK occurs and pass it to the doSomething function".
What you should do is base the argument on the image object itself. Every DOM object knows where it is in the DOM tree, so at the time it's clicked, the onclick code should use DOM traversal operations to figure out which div it's inside of and dynamically retrieve its ID. That way you don't have to worry about binding variables and scoping issues... just figure out which div contains your image and get the ID at run time.
尝试:
希望有帮助
Try:
Hope it helps