javascript 带有 onClick 函数的 AppendChild 链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12501042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:21:13 来源:igfitidea点击:
AppendChild Link with onClick function
提问by John
I'm trying to use appendChild within Javascript to create a link that has a onClick attribute. I can't seem to make it work or find how to do this simple task.
我正在尝试在 Javascript 中使用 appendChild 创建一个具有 onClick 属性的链接。我似乎无法让它工作或找到如何完成这个简单的任务。
var link=document.createElement("a");
link.appendChild(document.createTextNode("Link"));
link.href = '#';
link.onclick = 'loadScript()';
document.body.appendChild(link);
回答by Claudio Redi
回答by Maral Azizi
Try this:
试试这个:
window.onload = function () {
var files = document.getElementById('files');
document.getElementById('addLink').onclick = function () {
var file = document.createElement('input');
file.type = 'file';
files.appendChild(file);
};
};