这个声明有效。单击节点时,我会收到一条显示其 ID 的消息。
newnode.setAttribute("onClick", "alert(this.id)");
我需要将 this.id 的值传递给另一个函数,并且正在努力绕过它。
尝试 1:
以下不起作用;我知道我不能将 this.id 传递给另一个函数,它做同样的事情,因为 this 与测试函数中的任何内容都不相关:
newnode.setAttribute("onClick", "test(this.id)");
function test(f){
alert(f);
}
尝试 2:
var testvar = newnode.id;
newnode.setAttribute("onClick", "test(testvar)");
function test(f){
alert(f);
}
为什么 testvar 在我的 setAttribute 行中无法识别?
最佳答案
newNode.onclick = function(){
test(this.id);
};
function test(id){
alert(id);
}
关于Javascript 将元素 ID 属性作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14187322/