我正在开发一个自定义的dojo-widget。在小部件中,我动态地创建了一些DOM节点并将函数连接到它们的事件。为什么连接的onclick事件不会触发?
我的代码看起来与此类似:
var treeItem = document.createElement("div");
if (current.children) // 'current' is set up like: { id: <int>, name: <string>[, children: <id-array>] }
{
var treeItemExpander = document.createElement("img");
[...] // stuff like setting classes and setting the path, nothing special
dojo.connect(treeItemExpander, "onclick", function() { alert("test expander"); }); // problem line
treeItem.appendChild(treeItemExpander);
}
treeItem.innerHTML += current.name;
dojo.connect(treeItem, "onclick", function() { alert("test item"); });
this.tree.appendChild(treeItem);
现在,你也许可以从评论猜测,有条件的嵌套元素上的事件不会被触发。
即使当我注释掉treeItem
(顺便说一下)的连接时,我也没有成功。我试过treeItemExpander.onclick = function() {...}
也无济于事。唯一有效的,但在小部件中没有意义的是treeItemExpander.setAttribute("onClick", "alert('test');");
。
编辑:我做了这个问题一个小提琴:http://jsfiddle.net/YCJ6X/
编辑:jovica(在道场-IRC)发现,在Chrome浏览器不会出现在Ubuntu的问题。
那么如何获得附加到该图像的事件?
您是否尝试过使用字符串“点击”,而不是“onclick”事件的事件名称? *编辑*没关系,道场似乎与“上”很酷...... – Pointy