如何在javascript的setAttribute()方法中为函数添加参数
How to add parameters onto function in setAttribute() method in javascript
在javascript中,我正在创建新的图像标签,我正在使用setAttribute()方法为它们添加属性,但我发现,如果我添加一个onclick事件并添加一个函数,我不能为它设置一个参数,如下所示
count = 0; //Will be the number that will go into parameter for function
function start() {
imageTag = document.createElement("IMG"); //Creates image tag
imageTag.setAttribute("src", "popo.jpg"); //sets the image tags source
count++; //as the start function keeps getting called, count will increase by 1 and the parameter for each function in each individual image tag will increase by one
imageTag.setAttribute("onclick", "disappear(count)"); //this will add the onclick attribute with the function disappear() and the parameter inside which will be a number that will increase as the start function is called again and the image tag is called again *ERROR*
document.body.appendChild(imageTag); //appends the image tag created
}
问题是,当每个新的图像标签被创建时,它实际上只是创建
<img src = "popo.jpg" onclick = "disappear(count)"/>
我想让它更像
<img src = "popo.jpg" onclick = "disappear('1')"/>
<img src = "popo.jpg" onclick = "disappear('2')"/> //and so on...
在函数中添加count作为参数,而不是作为字符串。
count = 0; //Will be the number that will go into parameter for function
function start() {
imageTag = document.createElement("IMG"); //Creates image tag
imageTag.setAttribute("src", "popo.jpg"); //sets the image tags source
count++; //as the start function keeps getting called, count will increase by 1 and the parameter for each function in each individual image tag will increase by one
imageTag.setAttribute("onclick", "disappear("+count+")"); //this will add the onclick attribute with the function disappear() and the parameter inside which will be a number that will increase as the start function is called again and the image tag is called again *ERROR*
document.body.appendChild(imageTag); //appends the image tag created
}
不添加click event作为属性,使用addEventListener
imageTag.addEventListener("click", disappear.bind(null, count));
Bind使count
在函数调用时可用。
然后创建一个消失函数
function disappear(ID){
alert(ID); // This should give the count.
}
由于您使用setAttribute
,因此您正在设置事件处理程序内容属性。相应的事件处理程序设置为内部原始未编译处理程序。该代码的作用域为
- 全局对象文档
- 表单所有者(如果有的话)
- 元素(如果有)
如果你的count
变量在这些范围中不可用,它将无法工作。即使它是可用的,由于您不断增加它,使用的值将是修改后的值。
但是你不希望那样。相反,您可以:
-
在未编译的处理程序中写入变量:
imageTag.setAttribute("onclick", "disappear(" + count + ")");
-
使用事件处理程序IDL属性,例如
var localCount = count; // Using a local copy in case `count` is modified imageTag.onclick = function() { disappear(localCount); };
在参数函数中用''
代替""
::
imageTag.setAttribute("onclick", "disappear(" + count + ")");
后::
imageTag.setAttribute("onclick", "disappear('" + count + "')");
最佳实践::
imageTag.setAttribute("onclick", `disappear('${count}')`);
相关文章:
- 你能把一个匿名方法(函数)动态地变成一个命名方法吗
- 显示警报后调用方法/函数
- 使用来自 json 对象的参数动态调用方法/函数
- 创建具有动态参数排列的方法/函数
- 如何从其他 jsp 调用方法/函数以包含在另一个 jsp 的 jstree 中
- Objective-C 方法/函数调用.(来自JavaScript示例)
- 如何在android phongap应用程序中从javascript调用本机android方法(函数),而无需使用web
- 如何测试函数是否调用了特定的方法/函数
- 处理setTimeout(javascript)中的对象方法函数调用
- 简单级别,在DOM中选择元素,优化,创建方法函数
- 在不使用eval的情况下在方法函数中传递固定参数
- jQuery.html()方法函数文字参数
- animate方法函数执行意外延迟
- 对象方法javascript中的方法/函数
- Javascript私有方法:函数表达式与函数声明
- Jquery的oop重写方法函数
- 如何为数组/对象创建方法/函数
- 是否有可能发送我的对象与方法(函数)从服务器到客户端
- Javascript可以调用Django方法/函数吗
- 访问Backbone.js中的Ratchet 2.0方法/函数